Exclusive applications and Applications

Source: Internet
Author: User

Exclusive applications and Applications

// Problem description: There are two numbers that appear only once in the array, and the others appear in pairs. Please find out the numbers that appear only once.

Implementation Code:

Size_t FindFirstBitIs1 (size_t Num) // find a number (binary string) from right to left first 1 (for example: 14 -- 1110 returns 2 -- 10) {int IndexBit = 1; for (int I = 0; I <32; ++ I) {if (Num & IndexBit) return IndexBit; IndexBit <= 1 ;}} void FindNumsAppearOnce (int * arr, int lenth, int * Num1, int * Num2) // The main idea is that two numbers are equal or the result is zero {assert (arr); if (lenth <2) {cout <"incorrect input! "<Endl; return;} size_t ResultExclusiveOr = 0; for (int I = 0; I <lenth; ++ I) {ResultExclusiveOr ^ = arr [I];} size_t FirstBitIs1Num = FindFirstBitIs1 (ResultExclusiveOr); * Num1 = 0; * Num2 = 0; for (int I = 0; I <lenth; ++ I) {if (arr [I] & FirstBitIs1Num) * Num1 ^ = arr [I]; else * Num2 ^ = arr [I];}

 

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.