双指针法
- Runtime: 96 ms, faster than 65.76%
- Memory Usage: 39.3 MB, less than 26.93%
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
var i = 0
for(var j = 1; j < nums.length; j++) {
if(nums[i] !== nums[j]){
i++
nums[i] = nums[j]
}
}
return i+1
}
常规方法
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
for(var i = 0; i < nums.length; i++){
if(nums.indexOf(nums[i]) !== i){
nums.splice(i, 1)
i--
}
}
return nums.lenghth
};