【LeetCode】【Python题解】Single NumberII

Given an array of integers, every element appears three times except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

本题是一种极其巧妙的做法,参考的别人的,废话不多说,先上代码

class Solution:
    # @param A, a list of integer
    # @return an integer
    def singleNumber(self, A):
        ones,twos=0,0
        for ele in A:
            ones = ones^ele & ~twos
            twos = twos^ele & ~ones
        return ones

短短几行就可以解决这个看似不太简单的问题。首先需要对位进行操作这没有什么疑议了,关键是一般做法就是将每个数的对应位加起来,再对3取余数,最后将每一位的结果重新组成的数字就是最终只出现一次的(原因也很简单,就不多说了)

这种做法没有那么繁琐,而是非常巧妙的遍历一遍数组,时间复杂度为O(n),而且也没有占用额外的空间。

最关键的两行代码是:

ones = ones^ele & ~twos
twos = twos^ele & ~ones

当ele第一次出现时,因为ones和twos都初始化为0,所以很显然运行这两行代码后ones=ele,twos=0.

在此之后如果ele不再出现,那么很显然这就是只出现一次的数字,ones即为最终结果,如果它第二次出现,ones将会被清零,而twos则会储存下ele的值。

当其第三次出现时,ones和twos都会被清零,就像从未出现过该数字一样。

很神奇吧?!

时间: 2024-11-07 07:27:10

【LeetCode】【Python题解】Single NumberII的相关文章

Leetcode 位运算 Single NumberII

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number II Total Accepted: 14224 Total Submissions: 43648 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have

【LeetCode】【Python题解】Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 要求输入一个整数,返回一个表示杨辉三角的数组.我的方法是计算通项公式,首先是编写阶乘函数,然后计算C00,C10,C11即可 利用Python 的map嵌套可以很简洁地实现,核心代码只有一行! class So

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

LeetCode: palindromes 题解

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

LeetCode: LetterCombinations 题解

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode: plusOne 题解

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意: 给定一个由一组数字构成的数组表示的非负整数, 对这个数进行加一操作,并将结果数组返回. 数组的首元素存储的是该非负整数的最高位. 本题比较简

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

Pascal's triangle II Leetcode Python

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题和前面一题的区别在于不用返回所有解,只需要返回index对应行就行.做法和前面的一样定义一个currow 和prerow class Solu

Leetcode 位运算 Single Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number Total Accepted: 20063 Total Submissions: 44658 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear

Single Number && Single NumberII

这两个题,都是用bit operation解决的,但是第二个稍微tricky一点. 先说第一个,我们利用XOR, 如果一个数字出现两次,那么在每一位上,两两抵消.唯独那个只出现一次的,没有另一半和它抵消了..所以就剩下了. 1 public int singleNumber(int[] A) { 2 if (A == null) { 3 return 0; 4 } 5 int ret = 0; 6 for (int i = 0; i < A.length; i++) { 7 ret = ret