题目

https://leetcode.com/problems/single-number-iii/

题解:分组异或

参考1:讨论区题解
you know you can eliminate doubles with xor so do that first. whats left over is xor of the two single numbers. between those, there must be 1 bit that is set thats unique to that number. set a mask for that bit and xor again with only numbers that have that bit set. you will then find the first number. the first xor is xor of the two numbers. so xor it one more time with the first number to get the other number

参考2:官方题解

class Solution {public int[] singleNumber(int[] nums) {int xor = 0;for (int n : nums)xor ^= n;int a = xor;int b = xor;
//        int sn = -1; // 方法1:最高位1对应的索引位置
//        while (xor != 0) {//            xor >>>= 1;
//            sn++;
//        }
//        int mask = 1 << sn; // 选择最高位1所在的位置为maskint mask = 1; // 方法2:选择最低位1所在的位置为maskwhile ((mask & xor) == 0)mask = mask << 1;for (int n : nums) {if ((n & mask) == 0) a ^= n;else b ^= n;}return new int[]{a, b};}
}

leetcode 260. Single Number III | 260. 只出现一次的数字 III(位运算:分组异或)相关推荐

  1. 【leetcode】只出现一次的数字(位运算)

    LeetCode136:只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 解题思路: 这里可以使用异或运算.即两个相同的数字异或 ...

  2. 【?异或】LeetCode 260. Single Number III

    LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...

  3. leetcode 算法解析(一):260. Single Number III(C++版本和自己的注解)

    这个题来自<剑指offer>但是书上上感觉讲解不太详细,还是看博客吧(我把下面博客改写成了C++版本运行通过) 注意这个题的相关代码中,输入的数组只能有两个数出现一次,如果有第三个数出现一 ...

  4. Java实现 LeetCode 260 只出现一次的数字 III(三)

    260. 只出现一次的数字 III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出 ...

  5. [勇者闯LeetCode] 136. Single Number

    [勇者闯LeetCode] 136. Single Number Description Given an array of integers, every element appears twice ...

  6. 【异或】LeetCode 137. Single Number II

    LeetCode 137. Single Number II Solution1:不会做,抄的 博客转载自:http://www.cnblogs.com/grandyang/p/4263927.htm ...

  7. 【异或】LeetCode 136. Single Number

    LeetCode 136. Single Number Solution1:我的答案 还好异或的性质没记错,还好,还好 class Solution { public:int singleNumber ...

  8. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  9. LeetCode 260. 只出现一次的数字 III(位运算)

    1. 题目 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 :输入: [1,2,1,3,2,5] 输出: [3,5] 注意: 结 ...

最新文章

  1. 字符串转json对象的各种方法
  2. P6222 「P6156 简单题」(反演 + 积性函数线性筛)
  3. 如何自学web安全(详细路径)
  4. 进程相关概念与多线程的基本使用
  5. 关于System.FormatException异常
  6. 人工智能语音识别ppt
  7. SFML1 俄罗斯方块代码解析
  8. 修改卡巴斯基注册表,无限试用
  9. Apizza在线接口调试文档工具如何方便的设置线上线下环境变量
  10. Web开发的一点心得(二年工作总结版)
  11. python怎么才能不挂科_如何才能在大学里不挂科?
  12. heu oj 1011 square
  13. POJ1753 Flip Game
  14. 电脑版敬业签记事本软件如何清除某个分类所有内容
  15. 京东之鉴:兄弟是兄弟,员工是员工!-千氪
  16. 清除win+r的记录
  17. 新库上线 | CnOpenDataA股上市公司IPO申报发行文本数据
  18. 计算机可以不需要显卡吗,显卡有什么用 电脑不装显卡影响大吗
  19. 模型量化压缩功能全解:打开AI部署加速器
  20. 传记-《成为米歇尔·奥巴马》书中的精髓:米歇尔·奥巴马是怎样克服种族歧视、冲破阶层天花板,成为美国首位非洲裔第一夫人的。

热门文章

  1. MATLAB插值问题
  2. HDU - 3374 String Problem(最小表示法+最大表示法+KMP的next数组)
  3. HDU - 1847 Good Luck in CET-4 Everybody!(sg函数,水题)
  4. Mysql当前模式让不记录日志_MySQL日志binlog的三种模式
  5. Win10任务计划+PowerShell实现Git自动推送
  6. mfc 改变tab快捷键对控件的顺序
  7. 玩转Google开源C++单元测试框架Google Test系列(gtest)之六 - 运行参数
  8. strace 哇,好多系统调用
  9. Redis 过期键删除策略、内存淘汰机制
  10. Docker常用操作命令(二)