码迷,mamicode.com
首页 > 编程语言 > 详细

Java [Leetcode 137]Single Number II

时间:2016-04-03 18:59:37      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:

Given an array of integers, every element appears three times except for one. Find that single one.

解题思路:

具体参考Detailed explanation and generalization of the bitwise operation method for single numbers

讲的实在是很全面,而且拓展到了一般的情况,膜!!

代码如下:

public class Solution{
	public int singleNumber(int[] nums){
		int x1 = 0;
		int x2 = 0;
		int mask = 0;

		for(int i : nums){
			x2 ^= x1 & i;
			x1 ^= i;
			mask = ~(x1 & x2);
			x2 &= mask;
			x1 &= mask;
		}
		
		return x1;
	}
}

  

Java [Leetcode 137]Single Number II

标签:

原文地址:http://www.cnblogs.com/zihaowang/p/5350294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!