LeetCode—剑指Offer:数组中重复的数字(临时数组)

剑指Offer:数组中重复的数字(简单)

2020年8月7日

题目来源:力扣

LeetCode—剑指Offer:数组中重复的数字(临时数组)_第1张图片

解题
简单的想法,用数组记录出现次数,若不为0则说明重复,直接返回这个值

class Solution {
    public int findRepeatNumber(int[] nums) {
        int len=nums.length;
        //新建一个长度为len的数组用来存放数字出现次数
        int[] occ=new int[len];
        for(int i=0;i<len;i++){
            if(occ[nums[i]]==0){
                occ[nums[i]]++;
            }
            else{
                return nums[i];
            }
        }
        return 0;
    }
}

LeetCode—剑指Offer:数组中重复的数字(临时数组)_第2张图片

你可能感兴趣的:(LeetCode)