标签:
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?
Subscribe to see which companies asked this question
1 public class Solution { 2 public int singleNumber(int[] nums) { 3 int[] bitcnt = new int[32]; 4 int ans = 0; 5 for(int i = 0; i< 32;i++){ 6 for(int n : nums){ 7 if(((n >> i) & 1) == 1){ 8 bitcnt[i]++; 9 } 10 } 11 ans |= (bitcnt[i]%3) << i; 12 } 13 return ans; 14 } 15 }
平常位运算 使用的太少了,本题可以用map解决,也可以使用异或运算解决。参见其他大牛的帖子:
http://www.cnblogs.com/springfor/p/3870863.html
http://blog.csdn.net/derrantcm/article/details/47745445
137. Single Number II java solutions
标签:
原文地址:http://www.cnblogs.com/guoguolan/p/5610195.html