Single Number II

https://leetcode.com/problems/single-number-ii/

https://leetcode.com/discuss/6632/challenge-me-thx

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?

 1 public class Solution {
 2     public static int singleNumber(int[] nums) {
 3         int len=nums.length;
 4         int one=0;int two=0;
 5         for(int i=0;i<len;i++){
 6             one=(one^nums[i])&~two;
 7             two=(two^nums[i])&~one;
 8         }
 9         return one;
10     }
11     public static void main(String[]args){
12     int[]nums={1,1,1,2,3,3,3};
13     System.out.println(singleNumber(nums));
14     }
15 }

 

posted @ 2015-05-07 11:01  打小孩  阅读(100)  评论(0编辑  收藏  举报