Home leetCode Day50
Post
Cancel

leetCode Day50

Find the Duplicate Number

題目:

1
有一個數字陣列,有n + 1的數字,數字的值為1~n,求陣列中重複的數字

解法:

1
建立一個陣列,用數字為key,值為bool,數字出現過設定為true,如果數字為true,代表重複出現
code

func findDuplicate(nums []int) int {
    exist := make([]bool, len(nums))

    for _, value := range nums {
        if exist[value] {
            return value
        }
        exist[value] = true
    }

    return 0
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags