首页 > 其他 > 详细

Single Number III

时间:2015-11-02 02:08:01      阅读:117      评论:0      收藏:0      [点我收藏+]

Given an array of numbers?nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given?nums = [1, 2, 1, 3, 2, 5], return?[3, 5].

?

public class Solution {
    public int[] singleNumber(int[] nums) {
        int a = 0;
        int b = 0;
        for (int i : nums) {
			a ^= i;
		}
        int rightOne = a & (~a + 1);
        for (int i : nums) {
			if ((rightOne & i) != 0) {
				b ^= i;
			}
		}
        return new int[]{b, a^b}; 
    }
}

?

Single Number III

原文:http://hcx2013.iteye.com/blog/2253555

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!