Single Number II, singlenumberii

Source: Internet
Author: User

Single Number II, singlenumberii

Given an array of integers, every element appearsThreeTimes has t for one. Find that single one.

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

Solution:

1. Using bitwise operations in binary, if n * 3 identical numbers are added, each bit of binary must be a multiple of 3.

2. The remaining separate number is the remainder of each digit divided by 3. Then, each digit is converted to a decimal number, and the sum is the separate number.

1 int singleNumber (int * nums, int numsSize) {2 int ary [32] = {0}; 3 int res = 0; 4 int I; 5 int j; 6 for (I = 0; I <32; I ++) 7 {8 for (j = 0; j <numsSize; j ++) 9 {10 ary [I] + = (nums [j]> I) & 1); 11 ary [I] = ary [I] % 3; 12} 13 res | = (ary [I] <I); 14} 15 return res; 16}

Turn: http://blog.csdn.net/feliciafay/article/details/19004479

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.