lintcode83- Single Number II- midium

Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.

Given [1,1,2,3,3,3,2,2,4,1] return 4

法1:加和掩码提取位,模三,加到结果里。int 数据共有32位,针对其中具体某一位,可以用掩码(>>操作和 &1操作)把所有数字的这一位都加起来。针对重复3次的数字,他们加的和%3必定为0(0*3 %3 ==0, 1*3 %3 ==0),那么所有的和%3得到的必定就是寻找的数在这一位上到底是1还是0了。把这一位<<,并且加到res变量返回即可。

public class Solution {
    /**
     * @param A : An integer array
     * @return : An integer
     */
    public int singleNumberII(int[] A) {
        // write your code here
        int res = 0;
        for (int i = 0; i < 32; i++){
            int rsBit = 0;
            for (int idx = 0; idx < A.length; idx++){
                int bit = A[idx] >> i;
                bit &= 1;
                rsBit += bit;
            }
            rsBit %= 3;
            res += rsBit << i;
        }
        return res;
    }
}

法2:利用三个变量分别保存各个二进制位上 1 出现一次、两次、三次的分布情况,最后只需返回变量一。

1. 代码使得这三个变量表现形式是: 某数第一次出现- 100,某数第二次出现-010,某数第三次出现- 111(后续抹去变为001)。所以如果某个数是只出现一次(3n+1),它的特征位会被保留在ones中,如果某个数是出现正好两次(3n+2),它的特征位会被保留在twos中。

2. 小心一下循环里要先处理2再处理1。

3. |= 是想做赋一处理,&= 是想做归零处理。^= 是想做“突出奇数次特征位,抹平偶数次特征位”处理。

public class Solution {
    /**
     * @param A : An integer array
     * @return : An integer
     */
    public int singleNumberII(int[] A) {
        // write your code here
        int ones = 0, twos = 0, threes = 0;

        for(int i = 0; i < A.length; i++){
            //某数第一次进来,ones twos threes  1 0 0
            //某数第二次进来,ones twos threes  0 1 0
            //某数第三次进来,ones twos threes先1 1 1, 后 0 0 1
            //某数第四次进来,ones twos threes  1 0 0
            //循环
            twos |= ones & A[i];
            ones ^= A[i];
            threes = ones & twos;
            //第三次来时抹去一二痕迹
            twos &= ~three;
            ones &= ~three;
        }

        return ones;
    }
}
时间: 2024-09-27 04:40:14

lintcode83- Single Number II- midium的相关文章

Single Number,Single Number II

Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you imp

[LeetCode][JavaScript]Single Number II

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? https://leetcode.com/

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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

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

Single Number II ——位操作

题意: 给定两个32位的整数 N 和 M,以及表示比特位置的 i 与 j .编写一个方法,将 M 插入 N,使得 M 从 N 的第 j 位开始,到第 i 位结束.假定从 j 位到 i 位足以容纳M. 输入:N = 10000101000,M = 10011,i = 2, j = 6 输出:N = 10001001100 解题思路 :  根据题意,我们将问题解决分为三个步骤: (1)将 N 中从 j 到 i 之间的位清零: (2)对 M 进行移位操作,M << i (3)合并 M 与 N . 为

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要复杂的多,

Single Number和Single Number II

1 Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解析: a ^ a = 0,a ^ 0 = a 所以对所

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So