http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
class Solution {
public:
int removeDuplicates(int A[], int n) {
int current=0;
for(int i=0;i<n;i++){
if((i+2)<n&&A[i]==A[i+2]) continue;// Don't use A[i]==A[i-2], because A is changing
A[current++]=A[i];
}
return current;
}
};
本文介绍了一种去除有序数组中重复元素的方法,通过遍历数组并只保留最多两个连续相同元素的方式,有效地解决了LeetCode上的Remove Duplicates from Sorted Array II问题。此算法通过检查当前元素与其后两个元素的关系来决定是否保留当前元素。
748

被折叠的 条评论
为什么被折叠?



