"Leetcode": Single number III problem in go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Given An array of numbers nums , in which exactly-elements appear only once and all the other elements appear exactly Twice. Find the elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5] , return [3, 5] .

Note:

    1. The order of the result is not important. The above example, is [5, 3] also correct.

    1. Your algorithm should run in linear runtime complexity. Could implement it using only constant space complexity?
The main idea of the topic is: Given an array, the number of the array in addition to two different numbers, the remaining number 20% pairs appear, now to find out the two numbers ---------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------ This problem is not easy to take in hand, referring to the results of others "http://www.jianshu.com/p/402622b8ad43" briefly say the idea of solving problems, first of all the number of different or operation, then the same will be offset, the result is two different number A and B of the XOR, this result as a diff then Ask (-diff & diff) This equation can get only a 1 of the number in the binary, the position of this 1 represents in this position, A and B are different, then you can follow this difference, divide all the numbers into two groups, the first set is the same as a at that location, and the second group is the same as b at that location.
Func Singlenumber (nums []int) []int {        diff: = 0 for    _, V: = range nums {        diff = diff ^ v    }    //Get a diff representation Bitwise XOR result of two different numbers    diff =-diff & diff    Result: = Make ([]int, 2)    for _, V: = Range Nums {        if diff & v = = 0 {            result[0] = result[0] ^ v         } else {            result[1] = result[1] ^ v        }    }    return result}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.