标签:ack space blog not elements solution 操作 with 没有
27. Remove Element【leetcode】
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.
题意:从数组中移除所有的给定的某个值,并返回数组新的长度;最后一句的意思是在新的长度后面的元素是任意的,没有顺序的要求,只要是之前数组中的元素即可,后面的元素无所谓。
1 public class Solution { 2 public int removeElement(int[] nums, int val) { 3 int len=nums.length; 4 int count = 0; 5 for(int i=0;i<len;i++){ 6 if(nums[i]==val){ 7 for(int j=i;j<len-1;j++){ 8 nums[j]=nums[j+1]; 9 } 10 count++; 11 } 12 } 13 return len-count; 14 } 15 }
解题思路:
标签:ack space blog not elements solution 操作 with 没有
原文地址:http://www.cnblogs.com/haoHaoStudyShare/p/7337047.html