' Leetcode with Python ' single number Ii__python

Source: Internet
Author: User
Tags time limit
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 
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.