leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)
2014-07-02 20:51
489 查看
题目:
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 A =
Your function should return length =
说明:
1)无
实现:
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 A =
[1,1,2],
Your function should return length =
2, and A is now
[1,2].
说明:
1)无
实现:
class Solution { public: int removeDuplicates(int A[], int n) { if(n==0) return 0; int B ,k=0; for(int i=0;i<n;i++) B[i]=0; B[0]=A[0]; for(int i=1;i<n;i++) { if(B[k]!=A[i]) B[++k]=A[i]; } for(int i=0;i<=k;i++) A[i]=B[i]; return k+1; } };
相关文章推荐
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Leetcode26:Remove Duplicates from Sorted Array(移除已排序数组中重复的数字)
- [LeetCode]14. Remove Duplicates from Sorted Array排序数组去重
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- leetcode:Remove Duplicates from Sorted Array去掉排序数组中重复的元素
- LeetCode 26 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(消除已排序数组中的重复元素)
- leetcode 排序数组去重复并返回新数组长度Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array---leetcode 我的java题解
- LinCode Remove Duplicates from Sorted Array 删除排序数组中的重复数字
- 【leetcode 数组】Remove Duplicates from Sorted Array II
- leetcode 题解 || Remove Duplicates from Sorted Array 问题
- LintCode Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
- LeetCode题解——Remove Duplicates from Sorted Array