[LintCode]Remove Duplicates from Sorted Array
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// 2015-4-12
if (nums == null || nums.length == 0) {
return 0;
}
int index = 0; // 输出数组 的尾元素的序号
for (int i = 0; i < nums.length; i++) {
if (nums[index] != nums[i]) {
index++;
nums[index] = nums[i];
}
}
return index + 1;
}
}
本文介绍了一种从有序数组中去除重复元素的方法,并提供了一个Java实现示例。该方法通过遍历数组并只保留不重复的元素来工作,最终返回处理后的数组长度。
350

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



