[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?

这道题是之前那道单独的数字的延伸,那道题的解法就比较独特,是利用计算机按位储存数字的特性来做的,这道题就是除了一个单独的数字之外,数组中其他的数字都出现了三次,那么还是要利用位操作来解此题。我们可以建立一个32位的数字,来统计每一位上1出现的个数,我们知道如果某一位上为1的话,那么如果该整数出现了三次,对3去余为0,我们把每个数的对应位都加起来对3取余,最终剩下来的那个数就是单独的数字。代码如下:

class Solution {
public:
    int singleNumber(int A[], int n) {
        int res = 0;
        int count[32];
        for (int i = 0; i < 32; ++i) {
            count[i] = 0;
            for (int j = 0; j < n; ++j) {
                if ((A[j] >> i) & 1) count[i] = (count[i] + 1) % 3;
            }
            res |= (count[i] << i);
        }
        return res;
    }
};

还有一种解法,思路很相似,用3个整数来表示INT的各位的出现次数情况,one表示出现了1次,two表示出现了2次。当出现3次的时候该位清零。最后答案就是one的值。

  1. ones   代表第ith 位只出现一次的掩码变量
  2. twos  代表第ith 位只出现两次次的掩码变量
  3. threes  代表第ith 位只出现三次的掩码变量
class Solution {
public:
    int singleNumber(int A[], int n) {
        int one = 0, two = 0, three = 0;
        for (int i = 0; i < n; ++i) {
            two |= one & A[i];
            one ^= A[i];
            three = one & two;
            one &= ~three;
            two &= ~three;
        }
        return one;
    }
};
时间: 2024-10-12 08:46:05

[LeetCode] Single Number II 单独的数字之二的相关文章

[LeetCode] Single Number III 单独的数字之三

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order

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

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

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? 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 单元素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] 260. Single Number III 单独数 III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is