Single Number II
2014-01-09 22:09
183 查看
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?
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
public class Solution { public int singleNumber(int[] A) { // Note: The Solution object is instantiated only once and is reused by each test case. if(A.length == 0) return 0; int[] cnt = new int[32]; for(int i = 0; i < A.length; i++) { for(int j = 0; j < 32; j++) { if( (A[i]>>j & 1) ==1) { cnt[j] = (cnt[j] + 1)%3; } } } int res = 0; for(int i = 0; i < 32; i++) { res += (cnt[i] << i); } cnt = null; return res; } }
相关文章推荐
- Single Number II
- Single Number II
- leetcode:Single Number II
- LeeCode-Single Number II
- leetcode 之 Single Number II
- 【Leetcode】【Medium】Single Number II
- Single Number II
- leetcode 137 —— Single Number II
- (java) Single Number II
- Single Number && Single Number II && Factorial Trailing Zeroes && Pow(x, n) && Add Two Numbers
- Single Number II
- Single Number II
- [题解][LeetCode][Single Number II]
- 137. Single Number II
- Single Number II
- LeetCode 137 Single Number II
- 【Leetcode】:Single Number II问题 in Go语言
- [leetcode] Single Number II
- SINGLE NUMBER I & II & III
- Single Number II