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

3
为什么Leetcode编译器不接受这段代码?我的程序在NetBeans中运行,但是Leetcode编译器不接受它。
这是我的代码:
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;
}

这是我的主要方法:

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 + " ");
}

}

错误信息为:

无法将int[]类型转换为int类型。


这个Leetcode工具使用哪个版本的Java? - m.antkowicz
3个回答

3
我希望LeetCode在这一行代码上遇到了问题:

int new_nums[] = removeDuplicates(nums);

这不是定义整数数组的典型方式(这是C语言风格)。Java支持这种语法,但它有点神秘。请参见此答案获取更多细节

可以尝试这个:

int[] new_nums = removeDuplicates(nums);

在LeetCode上试过这个方法,似乎有效:

OP的代码在LeetCode上运行

我在LeetCode上尝试了您提供的代码,一切看起来都没问题。您能否提供更多关于您遇到的错误的信息? - Michael Powers

0

我会自己这样做:

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;
    }
}

0
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]"));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接