leetcode--Remove Duplicates from Sorted Array
2017-08-08 09:04
337 查看
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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 =
题意:给定一个排序数组,移除重复元素,返回新数组的长度。
分类:数组,双指针
解法1:双指针,一个用于遍历,一个用于指向当前覆盖位置。
[java] view
plain copy
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length==0) return 0;
int pre = nums[0];
int count = 1;
for(int i=1;i<nums.length;i++){
if(pre!=nums[i]){
pre = nums[i];
nums[count] = nums[i];
count++;
}
}
return count;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/45584177
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 ofnums being
1and
2respectively. It doesn't matter what you leave beyond the new length.
题意:给定一个排序数组,移除重复元素,返回新数组的长度。
分类:数组,双指针
解法1:双指针,一个用于遍历,一个用于指向当前覆盖位置。
[java] view
plain copy
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length==0) return 0;
int pre = nums[0];
int count = 1;
for(int i=1;i<nums.length;i++){
if(pre!=nums[i]){
pre = nums[i];
nums[count] = nums[i];
count++;
}
}
return count;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/45584177
相关文章推荐
- leetcode-26. Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode25:Remove Duplicates from Sorted Array
- [leetcode]Remove Duplicates from Sorted Array II
- [Leetcode] 26. Remove Duplicates from Sorted Array 解题报告
- [LeetCode]26. Remove Duplicates from Sorted Array I&II
- 【leetcode】Remove Duplicates from Sorted Array II
- leetcode--26. Remove Duplicates from Sorted Array
- leetcode: 80. Remove Duplicates from Sorted Array II
- Leetcode:26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode Remove Duplicates from Sorted Array
- leetcode Remove Duplicates from Sorted Array
- LeetCode 26: Remove Duplicates from Sorted Array
- 【Leetcode】26. Remove Duplicates from Sorted Array
- Leetcode[26]-Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array II
- 【Leetcode】之Remove Duplicates from Sorted Array II
- LeetCode Remove Duplicates from Sorted Array II