Leetcode: Number of bachelors found

Source: Internet
Author: User

Leetcode: Number of bachelors found

Question:

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

Note:
Your algorithm shocould have a linear runtime complexity. cocould you implement it without using extra memory?


Ideas:

I have heard of this question. The bitwise operator may think of two equal numbers for an exclusive or operation and the result is zero, zero and any non-zero number exclusive or the operation result is the non-zero number itself. Based on this, all elements in the array are connected in series for an exclusive or, those pairing changes to zero, and the final order must be different from zero, and the result must be its own. It doesn't matter if the order of XOR is normal? It doesn't matter. Exclusive or operations have a combination of laws and do not care about order, so you don't need to care about this detail.

Code:

Class Solution {public: int singleNumber (int A [], int n) {int re; for (int I = 0; I

 

  

Related Article

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.