Leetcode:Single Number II
2014-12-15 21:25
281 查看
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?
To solve this problem using only constant space, you have to rethink how the numbers are being represented in computers -- using bits.
If you sum the ith bit of all numbers and mod 3, it must be either 0 or 1 due to the constraint of this problem where each number must appear either three
times or once. This will be the ith bit of that "single number".
A straightforward implementation is to use an array of size 32 to keep track of the total count of ith bit.
We can improve this based on the previous solution using three bitmask variables:
had appeared once.
had appeared twice.
had appeared three times.
When the ith bit had appeared for the third time, clear the ith bit
of both
0. The final answer will be the value of
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
To solve this problem using only constant space, you have to rethink how the numbers are being represented in computers -- using bits.
If you sum the ith bit of all numbers and mod 3, it must be either 0 or 1 due to the constraint of this problem where each number must appear either three
times or once. This will be the ith bit of that "single number".
A straightforward implementation is to use an array of size 32 to keep track of the total count of ith bit.
int singleNumber(int A[], int n) { int count[32] = {0}; int result = 0; for (int i = 0; i < 32; i++) { for (int j = 0; j < n; j++) { if ((A[j] >> i) & 1) { count[i]++; } } result |= ((count[i] % 3) << i); } return result; }
We can improve this based on the previous solution using three bitmask variables:
onesas a bitmask to represent the ith bit
had appeared once.
twosas a bitmask to represent the ith bit
had appeared twice.
threesas a bitmask to represent the ith bit
had appeared three times.
When the ith bit had appeared for the third time, clear the ith bit
of both
onesand
twosto
0. The final answer will be the value of
ones.
int singleNumber(int A[], int n) { int ones = 0, twos = 0, threes = 0; for (int i = 0; i < n; i++) { twos |= ones & A[i]; ones ^= A[i]; threes = ones & twos; ones &= ~threes; twos &= ~threes; } return ones; }
相关文章推荐
- [leetcode]Single Number II
- Single Number II (leetcode)
- leetcode136:single number I&II 及拓展
- leetcode Single Number I II III
- leetcode 之 Single Number II
- (复习)[LeetCode]Single Number II
- Leetcode Single Number II
- [ LeetCode ] Single Number II [4]
- Leetcode Single Number II
- LeetCode | Single Number II【转】
- LeetCode之Single Number II
- [LeetCode] Single Number II
- leetcode-----Single Number II
- leetcode(137)数组之Single Number II
- Leetcode Single Number II (面试题推荐)
- Leetcode之SingleNumber I/II/III
- [leetcode]Single Number II
- 【leetcode刷题笔记】Single Number II
- leetcode:Single Number II
- leetcode 刷题之路 84 Single Number II