Java/961. N-Repeated Element in Size 2N Array 重复 N 次的元素

题目


代码部分一(16ms)

class Solution {
    public int repeatedNTimes(int[] A) {
        int[] nums = new int[10001];
        int n = A.length/2;
        
        Arrays.fill(nums, 0);//可不要 初始默认0
        for(int i : A){
            nums[i]++;
        }
        for(int j = 0; j < nums.length; j++){
            if(nums[j] == n) return j;
        }
        
        
        return 0;
    }
}

代码部分二(10ms )

class Solution {
    public int repeatedNTimes(int[] A) {
        int[] nums = new int[10001];
        int n = A.length/2;
        
        
        for(int i : A){
            nums[i]++;
        }
        for(int j = 0; j < nums.length; j++){
            if(nums[j] == n) return j;
        }
        
        
        return 0;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38959715/article/details/85233750
今日推荐