首页 > 代码库 > [LeetCode]Remove Duplicates from Sorted Array

[LeetCode]Remove Duplicates from Sorted Array

题目:给定一个数组,要求去除数组中所有重复数字(仅保留一位)

算法:如下

public class Solution {
    public int removeDuplicates(int[] A) {
	    	int length = 0;
	    	for (int i=0; i<A.length; ++i) {
	    		if (0==length || A[i]!=A[length-1]) {
	    			A[length++] = A[i];
	    		}
	    	}
	    	return length;
	    }
}