Leetcode Single number II

Source: Internet
Author: User

The single number II Problem of leetcode problem solving

In a set of numbers, one number appears only once, and the rest appears three times to find the number that appears only once.

Note the point:

    • Linear time complexity
    • It's best not to apply for extra space

Example:

Input: Nums = [1, 1, 1, 2, 3, 3, 3]

Output: 2

Thinking of solving problems

If you do not request space, you can record the number of occurrences by three variables, note that the order should appear three times, two times, once again, if the reverse will occur repeated calculations (such as calculating the first number,,, one = one | num -> num two = two | one & num -> num three = two & num -> num And at this time obviously should not exist a bit has appeared two or three times). When the number of occurrences of a bit has reached three times, the bits are erased. The last thing left is the number that only appears once.

The above method is not very general, if the number of the other than a number appears n times, then the above method is not easy to solve. You can use an array of 32 elements (because the int value is 32 bits) to record the number of occurrences of each bit, and finally to take the remainder of each to N, and to put the remaining numbers together through the displacement operation. For Python, it is important to consider the case of negative numbers, because the Python default value can be infinitely large, instead of distinguishing positive and negative numbers based on whether the first is zero, and need to be resolved manually.

AC Source
 class solution(object):     def singlenumber(self, nums):        "" : Type Nums:list[int]: Rtype:int " " "One, both, three =0,0,0         forNuminchNums# Calculate the Count of the each bitthree = both & Num. One & num one = one | Num# Clear the count for the bit which have achieved threeone = one & ~three one = both & ~threereturnOne def singlenumber_normal(self, nums):result =0         forIinchRange +): Count =0             forNuminchNums:count + = (num >> i) &1rem = count%3            # deal with the negative situation            ifi = = to  andRem:result-=1<< to            Else: Result |= rem << ireturnResultif__name__ = ="__main__":assertSolution (). Singlenumber ([1,1,1,2,3,3,3]) ==2    assertSolution (). Singlenumber ([-2, -2,1,1, -3,1, -3, -3, -4, -2]) == -4    assertSolution (). Singlenumber_normal ([1,1,1,2,3,3,3]) ==2    assertSolution (). Singlenumber_normal ([-2, -2,1,1, -3,1, -3, -3, -4, -2]) == -4

Welcome to my GitHub (Https://github.com/gavinfish/LeetCode-Python) to get the relevant source code.

Leetcode Single number II

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.