leetcode-26 Remove Duplicates from Sorted Array
发布时间:2020-12-13 20:47:00 所属栏目:PHP教程 来源:网络整理
导读:问题描写: Givena sorted array,remove the duplicates in place such that each element appearonlyonceand return the new length. Do not allocate extra space for another array,youmust do this in place with constant memory. For example, Given i
问题描写: Givena sorted array,remove the duplicates in place such that each element appearonly once and return the new length. Do not allocate extra space for another array,youmust do this in place with constant memory. For example, Your function should return length = 2. 问题分析:使用1个count统计不同的元素个数,然后注意A[count] = A[i]来消除重复元素便可 代码: public class Solution {
public int removeDuplicates(int[] A) {
if(A == null || A.length == 0)
return 0;
int count = 1;
for(int i = 1; i < A.length; i++)
{
if(A[i] != A[i - 1])
{
//更新A[count]
A[count] = A[i];
//更新count
count ++;
}
}
return count;
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |