Single Number II (17)

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?

这一次数字重复出现的次数是3了,不像是 single number中的2,不能采取直接异或的办法了。难度上升了一点。其实只要这么想就行了,把所有的数直接看成二进制形式,如果一个数出现了3次,其每一个二进制位1就必须重复出现3次,而3%3=0。int数是4字节,对应32位,只要记录所有数二进制位出现的次数模三,就会得出那个特殊的数二进制位1出现的位置,这样就会得出那个特殊的数是谁。

按照上述思路写出代码:

class Solution {
public:
    int singleNumber(int A[], int n) {
        int bitnum[32]={0};
        int res=0;
        for(int i=0; i<32; i++){//外循环对应32个二进制位
            for(int j=0; j<n; j++){
                bitnum[i]+=(A[j]>>i)&1;//记录对应位置二进制位出现的次数
            }
            res|=(bitnum[i]%3)<<i;//还原特殊数第i+1个二进制位
        }
        return res;
    }
};

>> 和 << 代表右移和左移。例如:

a=a>>2;将a对应二进制数右移2位

a=a<<2;将a对应二进制数左移两位

请注意!(a>>i)&1取得的是a从右向左数第i个二进制位

扩展:

如果重复出现的数都出现了k次,那只需将上面的模3改为模k即可!

时间: 2024-10-11 03:49:26

Single Number II (17)的相关文章

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? 分析: 第一问属于技巧题,做过就会,

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

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