
1 2 3
|
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length.
|
给定一个数组和一个值,删除该值的所有实例,并返回新的长度。
元素的顺序可以被改变,也不关心最终的数组长度
思路:先排个序
加了一个position数组来存储位置相关的信息
一开始没注意是返回number,以为返回nums
加了个length就好啦
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
* @param {number[]} nums * @param {number} val * @return {number} */ function removeElement(nums,val) { function compare(num1,num2){ return num1 -num2; } nums.sort(compare); var position=[]; for(var i=0;i<nums.length;i++){ if(nums[i]==val){ position.push(i); } } nums.splice(position[0],position.length); return nums.length; }
|
近期评论