美文网首页
数组中只出现一次的数字 Go版

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

作者: 刺猬窝窝头 | 来源:发表于2019-05-08 00:06 被阅读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)
    

    相关文章

      网友评论

          本文标题:数组中只出现一次的数字 Go版

          本文链接:https://www.haomeiwen.com/subject/krfpoqtx.html