LeetCode oj 260. Single Number III (位运算)

260. Single Number III

 
  My Submissions
  • Total Accepted: 49443
  • Total Submissions: 103739
  • Difficulty: Medium

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].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
给你一个数组,数组里只有两个数是出现一次的,其他数字都是出现两次,要求用O(n)时间复杂度,O(1)空间复杂度的算法找出这两个数
要是按以前我acm的做法,直接就是map存次数,又因为map自动有序,所以输出map[0]和map[1]就行了,but it can‘t let 空间复杂度保证O(1),
通过这题学到了一种巧妙的利用位运算解题的思路。
众所周知,x ^ x = 0,所以先用异或遍历一遍数组,设要输出的两个数一个为x,一个为y,那么遍历之后得到的结果为x^y,因为这x,y两个数不同,所以必定会
有起码有一位在各自的对应位上不同,又因为是异或,所以x^y中为1的位就是x和y中对应位不同的位置。设ans = x ^ y,利用ans&(-ans)求出最低为1
的位是哪一位,至于为什么 ans&(-ans)可以求出来,不会的可以去学一下树状数组(甩锅= =+),如果实在理解不了的,就用while循环求吧。求出这个最低位
之后,我们就可以根据数组中的元素在这一位是1还是0来把他们分为两组,这样就可以保证x,y被分开,然后对这两组分别进行异或求结果。这题是
Special judge,return 的数组中的元素不要求保证顺序。
public class Solution {
    public int[] singleNumber(int[] nums) {
        int ans = 0;
        int a[] = new int [2];
        int index = 1;
        int len = nums.length;
        for(int i=0;i<len;i++){
            ans ^= nums[i];
        }
        index = ans & (-ans);
        for(int i=0;i<len;i++){
            if((nums[i] & index) == 0)
                a[0] ^= nums[i];
            else
                a[1] ^= nums[i];
        }
        return a;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值