MDGSF Software Engineer

[算法学习][leetcode] 136 Single Number

2017-10-11
mdgsf
Art

https://leetcode.com/problems/single-number/description/

题目

Given an array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

题目翻译

题目解析

参考答案

package main

import "fmt"

func singleNumber(nums []int) int {
    result := 0
    for _, num := range nums {
        result = result ^ num
    }
    return result
}

func main() {
    fmt.Println(singleNumber([]int{1, 1, 2, 3, 3}))
}

weixingongzhonghao

Similar Posts

Comments