Skip to content

Latest commit

 

History

History
37 lines (32 loc) · 739 Bytes

03.数组中重复的数字.md

File metadata and controls

37 lines (32 loc) · 739 Bytes

03. 数组中重复的数字

https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/

Java

class Solution {

    // 先排序后查找
    public int findRepeatNumber(int[] nums) {
        Arrays.sort(nums);
        for(int i = 1; i < nums.length; i++) {
            if(nums[i] == nums[i - 1]) {
                return nums[i];
            }
        }
        return -1;
    }
}

Python

class Solution(object):

    # 利用哈希表
    def findRepeatNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        hashSet = set()
        for num in nums:
            if num in hashSet: return num
            hashSet.add(num)
        return -1