首页 > 试题广场 >

有序数组删除重复数字

[编程题]有序数组删除重复数字
  • 热度指数:14814 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一个已排序的数组,使用就地算法将重复的数字移除,使数组中的每个元素只出现一次,返回新数组的长度。

不能为数组分配额外的空间,你必须使用常数级空间复杂度的就地算法。
例如,
给定输入数组 A=[1,1,2],
你给出的函数应该返回length=2,A数组现在是[1,2]。

public class Solution31 {

	public int removeDuplicates(int[] A) {
		// TODO Auto-generated constructor stub
		if(A==null||A.length==0)
			return 0;
		int one=0,two=1;
		int count=1;
		while(one<A.length&&two<A.length) {
			if(A[one]!=A[two]) {
				A[count++]=A[two];
			}
			one++;
			two++;
		}
		return count;
	}

}

发表于 2020-05-02 16:04:28 回复(0)
public class Solution {
    public int removeDuplicates(int[] arr) {
        if(arr.length==0||arr==null)
            return 0;
        int low = 1;
        for(int i=1;i<arr.length;i++){
            if(arr[i-1]==arr[i])
                continue;
            arr[low++]=arr[i];
        }
        return low;
    }
}


发表于 2019-08-29 16:50:43 回复(0)
import java.util.Arrays;

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

发表于 2019-07-02 18:16:35 回复(0)
public class Solution {
public int removeDuplicates(int[] A) {
int len = A.length;
if(len==0){
return 0;
}
int new_len = 0;
int i,j;
int flag = A[0];
for(i=1;i<len;i++){
int step =0;
j = i;
while(j<len&&A[j]==flag){
step++;
j++;
}
if(j==len){
break;
}
flag = A[j];
for(j=i;j<len&&j<i+step;j++){
A[j] = A[i+step];
}
}
flag = A[0];
new_len = 1;
for(i=1;i<len;i++){
if(A[i]!=flag){
new_len++;
flag = A[i];
}else{
break;
}
}
return new_len;
}
}
采用了一种十分复杂的方法,每次遇到重复的,则后面的数字向前进,过程十分曲折,结题思路不推荐!!!
下面算是正确的解题思路:

编辑于 2018-09-24 16:07:41 回复(0)
public class Solution {
    public int removeDuplicates(int[] A) {
        if(A.length==0 || A==null) return 0;
        int count=0;
        int i=0;
        while(i<A.length-1){
            if(A[i]==A[i+1]){
                count++;
                i++;
            }
            else{
                A[i-count+1] = A[i+1];
                i++;
            }
        }
        return A.length-count;
    }
}

发表于 2018-07-11 10:08:32 回复(0)
public class Solution {
    public int removeDuplicates(int[] A) {
        if (A == null || A.length == 0)
            return 0;
        if (A.length == 1)
            return 1;
        int index = 1;
        for (int i = 1; i < A.length; i++) {
            if (A[i] != A[i-1]) {
                A[index++] = A[i];
            }
        }
        return index;
    }
}
发表于 2018-04-02 18:48:05 回复(0)
public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) if (i == 0 || n > nums[i-1])
            nums[i++] = n; return i;
}

And to not need the i == 0 check in the loop:

public int removeDuplicates(int[] nums) { int i = nums.length > 0 ? 1 : 0; for (int n : nums) if (n > nums[i-1])
            nums[i++] = n; return i;
}
发表于 2017-03-12 23:24:42 回复(0)

问题信息

难度:
8条回答 21499浏览

热门推荐

通过挑战的用户

查看代码