数组中只出现一次的数字 Go版

2019-05-08  本文已影响0人  刺猬窝窝头
package main
import "fmt"

func SingleNumber(numbers []int) int {
    var tmp int = 0
    for _, v := range numbers {
        tmp ^= v
    }
    return tmp
}

func SingleNumberHelper(numbers []int) {
    result := SingleNumber(numbers)
    fmt.Printf("Find successfully, (%d) \n", result)
}

func main() {
    SingleNumberHelper([]int {1, 2, 2, 3, 3})
    SingleNumberHelper([]int {1, 2, 3, 3, 1})
    SingleNumberHelper([]int {3, 9, 3, 9, 3})
}

运行结果为

Find successfully, (1)
Find successfully, (2)
Find successfully, (3)
上一篇下一篇

猜你喜欢

热点阅读