Blog Domain name: Http://www.xnerv.wang
Original title page: https://oj.leetcode.com/problems/single-number-ii/
Topic Type: Bitwise operation
Difficulty Evaluation: ★★★
This article address: http://blog.csdn.net/nerv3x3/article/details/39453407
Given an array of integers, every element appears three times except for one. Find is single one.
Note:
Your algorithm should have a linear runtime. Could you implement it without using extra memory?
Compared to single number, the former is two occurrences of each element (except for a special element), and the subject is three occurrences of each element (except for a special element).
int data Total 32 bits, you can use 32 variables to store the n elements of each bits on the number of 1, and finally in the mode three operations, if 1, that means that this is to find the element binary representation of the 1. But this approach is not the best estimate, there should be better algorithms, thinking.
class Solution: # @param A, a list of integer # @return an integer def singlenumber (self, a): Len_a = Len (a) counts = [0] * res = 0 for-I in range (0): for num in A: #num = Long (num) # It'll make time limit exceeded counts[i] + = (num>>i) &A mp 1 res |= ((Counts[i]% 3) << i) if 0!= counts[31]% 3:res-= POW (2, 32) # # # return Res