leetCode: Single Number II [137]

【题目】

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?

【题意】

给定一个整数以外,其中除了一个整数只出现一次以外,其他都整数都出现3次,找出那个出现一次的数

要求线性复杂度,不适用额外的空间?

【思路】

模拟三进制运算

three, two, one     分别表示当前是否已经出现了3个1, 2个1, 1个1

0       0   0       表示没有出现1

0       0   1       表示出现了1个1

0       1   0       表示出现了2个1

0       1   1       表示出现了3个1,这时我们需要把它转化成

1       0   0       也就是3进制计算的结果,我们得到three=1,然后把two和one清0

各位的迭代关系如下:

two = (one & A[i]) | two    已经出现了一个1,这次又出现了一个1 或者 这次出现的不是1,但是本来就已经有两个1了

one = one ^ A[i]            如果本来就有一个1了,这次又出现一个1,那么这我们需要向two进一位(也就是上一步,将two设成1),这是我们需要将one清为0

three = two & one           如果已经出现了3个1,则three为1,此时需要将two和one清0

【代码】

class Solution {
public:
    int singleNumber(int A[], int n) {
        int one=0;
        int two=0;
        int three=0;

        for(int i=0; i<n; i++){
            two= (one & A[i]) | two;
            one= one ^ A[i];
            three = one & two;

            // three=1表示1已经出现了3此,two和one需要清空
            two &= ~three;
            one &= ~three;
        }
        return one;
    }
};

leetCode: Single Number II [137],布布扣,bubuko.com

时间: 2024-10-12 23:13:10

leetCode: Single Number II [137]的相关文章

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if

LeetCode&mdash;&mdash;Single Number II(找出数组中只出现一次的数2)

问题: 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?   Single Number I 升级版,一个数组中其它数出现了

[LeetCode] Single Number II 位运算

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? Hide Tags Bit Manipulation 数组中的数均出现3次,

LeetCode Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. 题意:有一个数组,只有一个数出现一次,其他的都出现三次,找出一次的数 思路:首先我们想到是每次把每一位二进制上1的个数都mod3,然后就能找出一个的了,但是这样空间太大了,所以我们想能记录每一次出现三次的时候就清0,那么我们需要先记录1次的,然后记录2次的,这样就能求出三次的了,最后再更新出现1次和

LeetCode——Single Number II

Description: Given an array of integers, every element appears three times except for one. Find that single one. 只有一个出现一次的数字,其他的都出现了3次,找出出现一次的那个数字. public class Solution { public int singleNumber(int[] nums) { Map<Integer, Integer> map = new HashMap

[LeetCode] Single Number II 单独的数字之二

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? 这道题是之前那道单独的数字的延伸,那道题的解法就比较独特,是利用计算机按位储

LeetCode Single Number II 单元素2

题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这个位肯定为1. 22ms 1 class Solution { 2 public: 3 int singleNumber(vector<int>& nums) { 4 int times[32]={0}; 5 for(int i=0; i<nums.size(); i++) 6 fo

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/single-number-ii/ 题目描述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 co

LeetCode 137:Single Number II

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? Single Number II 比Single Number要复杂的多,