简体   繁体   English

Leetcode:从排序数组中删除重复项

[英]Leetcode : Remove Duplicates from Sorted Array

Why this code is not accepted by Leetcode compiler?为什么这段代码不被 Leetcode 编译器接受? My program is running in.netbeans.我的程序在.netbeans 中运行。 but leetcode compiler is not accepting it.但是 leetcode 编译器不接受它。

Here is my code:这是我的代码:

import  java.util.Arrays;


public class RemoveDuplicateFromArray {

public static int[] removeDuplicates(int[] nums) {

    int temp, count = 0 ,l = 0;
    temp = nums[0];

  for(int i = 1; i < nums.length - (1 + l); i++ ) {

      if(temp == nums[i]) {
          count = count + 1;

          for(int j = i; j < nums.length - count; j++){
              nums[j] = nums[j+1];
            }

          i = i - 1;

      } else {
          temp = nums[i];
          i = i ;

      }
       l = count;
  }

      nums = Arrays.copyOfRange(nums, 0, nums.length-count);

      if(nums.length == 2){
      if(nums[0] == nums[1]){
          nums = Arrays.copyOfRange(nums, 0, nums.length-1);
      }
  } 

    return  nums;
}

Here is my main mathod:这是我的主要方法:

public static void main(String[] args) {

    int[] nums = new int[] {1,1,1}; // showing error here.
    System.err.println("nums lenght: " +nums.length);

    int new_nums[] = removeDuplicates(nums);

   for(int i : new_nums) {
        System.err.print(i + " ");
}

}

} }

The error is:错误是:

incompatible types: int[] cannot be converted to int.不兼容的类型:int[] 无法转换为 int。

I expect leetcode is having trouble with this line:我希望 leetcode 在处理这一行时遇到问题:

int new_nums[] = removeDuplicates(nums);

This isn't the typical way to define an integer array (this is the C style).这不是定义 integer 数组的典型方法(这是 C 样式)。 Java does support the syntax, but it's a little arcane. Java 确实支持语法,但有点神秘。 See this answer for more detail .有关详细信息,请参阅此答案

Try this instead:试试这个:

int[] new_nums = removeDuplicates(nums);

Just tried this on LeetCode, seems to work:刚刚在 LeetCode 上试过这个,似乎有效: OP 的代码跑在 LeetCode 上

I would do it a bit like this myself:我自己会这样做:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        int[] nums = new int[]{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9, 0, 0, 0, 0}; 
        System.out.println("nums lenght: " + nums.length);

        int[] new_nums = removeDuplicates(nums);

        for (int i : new_nums) {
            System.out.print(i + " ");
        }

    }
    public static int[] removeDuplicates(int[] nums) {
        List<Integer> found = new ArrayList();
        for (int i = 1; i < nums.length; i++) {
            if (!found.contains(nums[i])) {
                found.add(nums[i]);
            }
        }
        int[] ret = new int[found.size()];
        for(int i = 0; i < found.size(); i++){
            ret[i] = found.get(i);
        }
        return ret;
    }
}
public class Solution {
    public int removeDuplicates(int[] nums) {
        int n = nums.length;
        int i = 0;
        int j = 1;
        if (n <= 1) {
            return n;
        }
        while (j <= n - 1) {
            if (nums[i] != nums[j]) {
                nums[i + 1] = nums[j];
                i++;
            }
            j++;
        }
        return i + 1;
    }
}
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import org.junit.jupiter.api.Test;

class SolutionTest {
    @Test
    void removeDuplicates() {
        int[] array = new int[] {1, 1, 2};
        int end = new Solution().removeDuplicates(array);
        assertThat(Arrays.toString(Arrays.copyOfRange(array, 0, end)), equalTo("[1, 2]"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM