LeetCode oj 260. Single Number III (位运算)
来源:互联网 发布:淘宝新店有扶持吗 编辑:程序博客网 时间:2024/05/29 19:05
260. Single Number III
- 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:
- The order of the result is not important. So in the above example,
[5, 3]
is also correct. - 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
- LeetCode oj 260. Single Number III (位运算)
- [leetcode][位运算] Single Number III
- leetcode 260. Single Number III-寻找单身狗|位运算
- LeetCode oj 136. Single Number (位运算)
- leetcode oj java 260. Single Number III
- 位操作 leetcode 260. Single Number III
- leetcode 260. Single Number III 位操作
- LeetCode 260 Single Number III (位运算 推荐)
- 【LeetCode OJ 260】Single Number III
- Leetcode 位运算 Single Number
- LeetCode【位运算】:Single Number
- leetcode Single Number III 也是位操作
- [leetcode] 260.Single Number III
- Leetcode 260. Single Number III
- 260. Single Number III LeetCode
- [LeetCode]260. Single Number III
- LeetCode 260. Single Number III
- LeetCode 260. Single Number III
- HDU-5914 Triangle(思路)
- mybatis笔记
- Android 通知栏信息
- 人品问题
- KWIC问题:tokenizer/arraylist in java
- LeetCode oj 260. Single Number III (位运算)
- Glide图片加载框架学习
- 【Linux专题(三)—— 机器学习】小白也能玩tensorflow作梵高画
- 工具分享:Android投影控制软件推荐——Total Control
- Mybatis传多个参数如int 和String该怎么传
- 闭包
- R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO, SSD系列深度学习检测方法梳理
- IOS 动画设计(4)——maskView与CAGradientLayer相配合实现蒙板动画
- 找工作的那点事