算法系列(4) leetcode26
2017-10-29 10:59
288 查看
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums =
Your function should return length =
It doesn't matter what you leave beyond the new length.
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
for(var i=0; i<nums.length-1;i++){
if(nums[i+1]==nums[i]){
nums.splice(i,1);
i--;
}
}
};
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums =
[1,1,2],
Your function should return length =
2, with the first two elements of nums being
1and
2respectively.
It doesn't matter what you leave beyond the new length.
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
for(var i=0; i<nums.length-1;i++){
if(nums[i+1]==nums[i]){
nums.splice(i,1);
i--;
}
}
};
相关文章推荐
- 算法系列(17) Leetcode 104. Maximum Depth of Binary Tree
- 算法系列(3)leetcode27
- 算法系列(5) leetcode350
- [LeetCode]Array主题系列{1,11,15,16,18,26,27,31,33,34题}
- 算法系列(19)Leetcode 344. Reverse String
- 算法系列(15)LeetCode 171
- 算法系列(13) Leetcode 169 Majority Element
- 【Leetcode】算法题26 Remove Duplicates from Sorted Array
- 算法练习专题——LeetCode系列之 ThreeSum
- 算法细节系列(26):区间
- python写算法题:leetcode: 26. Remove Duplicates from Sorted Array
- 算法系列(18) Leetcode 496. Next Greater Element I
- 算法系列(9) LeetCode256
- 算法练习专题——LeetCode系列之 Two Sum
- 算法系列(20) Leetcode 575. Distribute Candies
- 算法作业HW25:LeetCode 26. Remove Duplicates from Sorted Array
- 算法系列(10)LeetCode520 Detect Capital
- 算法系列(7)leetcode 206
- 算法系列(16)Leetcode 387 First Unique Character in a String
- 算法系列(14) Leetcode 492. Construct the Rectangle