Given an array and a value, remove all instances of that value in place and return the new length.
Do the allocate extra space for another array, and you must does this on place with constant memory.
The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3]
, val =3
Your function should return length = 2, with the first of the elements of nums being 2.
Test instructions: Eliminates all specified values in the array
1 classSolution (Object):2 def removeelement (self, Nums, val):3 """4: Type nums:list[int]5: Type val:int6: Rtype:int7 """8 Nums.sort ()9CN =Nums.count (val)Ten ifCN! =0: Onei =Nums.index (val) A forCinchRange (CN): - ifnums[i]==Val: - del Nums[i] then =Len (nums) - returnN
1 intRemoveelement (int* Nums,intNumssize,intval) {2 if(nums==NULL)3 return 0;4 intI=0, j=0;5 for(i=0; i<numssize;i++){6 if(nums[i]!=val) {7nums[j]=Nums[i];8J + +;9 }Ten } One returnJ; A}
"Leetcode" 27. Remove Element