"From zero single Brush Leetcode"

Source: Internet
Author: User

Yes, I'm Wu. 2009 fans, from today, imitate the "from zero single row" series, Vegetable chicken single brush leetcode!

Topic:

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

Answer:

is actually looking for different. This problem requires a more trick idea: bitwise arithmetic .

Imagine, any two of the same number, to do the XOR or operation answer is what? is 0. 4^8^4^8 = 0. The same is true for N-bits in the middle.

0 with any number of different or operation, what is the answer? is not this number itself.

Class Solution {public:    int singlenumber (int a[], int n) {        int sum = a[0];        for (int i = 1; i<n; i++)            sum = sum ^ a[i];        return sum;    }};


"From zero single Brush Leetcode"

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.