去掉排序数组中重复的数字
2013-04-24 14:26
218 查看
题目:
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 =
分析:此题比较简单,可以利用类似快排的思想。
具体见如下代码:
int removeDuplicates(int A[], int n) {
if(n<=1)return n;
int j=0;
for(int i=1;i<n;i++)
{
if(A[i]!=A[j])
{
A[++j]=A[i];
}
}
return j+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].
分析:此题比较简单,可以利用类似快排的思想。
具体见如下代码:
int removeDuplicates(int A[], int n) {
if(n<=1)return n;
int j=0;
for(int i=1;i<n;i++)
{
if(A[i]!=A[j])
{
A[++j]=A[i];
}
}
return j+1;
}
相关文章推荐
- JAVA去掉一个已排序数组的重复数字
- java__排序后的数组删除重复数字。给定升序排序的数组,如果数组有 22 个或 22 个以上相同的数字,去掉他们,直到剩下 22 个为止。
- LintCode【简单】100. 删除排序数组中的重复数字。代码及思路
- 计蒜客题目 排序后的数组删除重复数字
- 删除排序数组中的重复数字
- LintCode- 删除排序数组中的重复数字
- 去掉数组中重复的数字(+冒泡排序语法)
- 请把一个整形数组中重复的数字去掉
- lintcode--删除排序数组中的重复数字
- 计蒜客:排序后的数组删除重复数字
- LintCode(100)删除排序数组中的重复数字
- 删除排序数组中的重复数字Ⅱ
- Java实现-删除排序数组中的重复数字1
- LintCode 第100题 删除排序数组中的重复数字
- 删除排序数组中的重复数字
- 删除排序数组中的重复数字 II
- lintcode-删除排序数组中的重复数字II-101
- LintCode:删除排序数组中的重复数字
- 把一个整形数组中重复的数字去掉 - 微软面试题
- LinCode Remove Duplicates from Sorted Array 删除排序数组中的重复数字