【Leetcode】算法题26 Remove Duplicates from Sorted Array
2017-09-28 21:00
609 查看
题目:
原题链接:Remove Duplicates from Sorted Array
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 =
with the first two elements of nums being
It doesn't matter what you leave beyond the new length.
贴代码:
int removeDuplicates(int* nums, int numsSize) {
if (numsSize == 0)
return 0;
int i = 0;
int rest = 0;
int first = nums[0];
for (i = 0; i < numsSize; i++) {
if (nums[i] != first){
rest++;
first = nums[i];
nums[rest] = nums[i];
}
}
return rest + 1;
}时间排名:
原题链接:Remove Duplicates from Sorted Array
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 =
[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.
贴代码:
int removeDuplicates(int* nums, int numsSize) {
if (numsSize == 0)
return 0;
int i = 0;
int rest = 0;
int first = nums[0];
for (i = 0; i < numsSize; i++) {
if (nums[i] != first){
rest++;
first = nums[i];
nums[rest] = nums[i];
}
}
return rest + 1;
}时间排名:
相关文章推荐
- python写算法题:leetcode: 26. Remove Duplicates from Sorted Array
- 算法作业HW25: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]【数组】26. Remove Duplicates from Sorted Array
- leetcode-26. Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array——python(easy)
- [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(去掉数组重复数字,常数空间限制)【面试算法题】
- leetcode26: Remove Duplicates from Sorted Array
- 【LeetCode-面试算法经典-Java实现】【026-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 26. Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array