数组中重复的数字

2020-06-10  本文已影响0人  上杉丶零
package 剑指Offer.数组中重复的数字;

public class LeetCode {
    public static void main(String[] args) {
        System.out.println(new Solution().findRepeatNumber(new int[] {2, 3, 1, 0, 2, 5, 3}));
    }
}

class Solution {
    public int findRepeatNumber(int[] nums) {
        int[] is = new int[nums.length];

        for (int num : nums) {
            is[num]++;

            if (is[num] > 1) {
                return num;
            }
        }

        return -1;
    }
}
image.png
上一篇 下一篇

猜你喜欢

热点阅读