首页 > 编程语言 > 详细

Java [Leetcode 137]Single Number II

时间:2016-04-03 18:57:22      阅读:180      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!