题目描述:

给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出

原文描述:

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?

思路:

  • 考虑使用位运算
    -那数组分为两组,每个组只有一个出现一次的数字,单独进行异或处理,考虑用全部异或的方式

  • 假设得到异或的结果是A,由于两个数字不同。A肯定有一个位是1,找到那个位,然后用来把这个数组分为两组

  • 其中一个组的异或结果是B,输出【B,A ^ B】

代码:

public class Solution {public int[] singleNumber(int[] nums) {int xOne = 0;for (int x : nums) {xOne ^= x;}// 获取第一个位1的索引int k = 0;for (k = 0; k < Integer.SIZE; ++ k) {if (((xOne >>> k) & 1) == 1) {break;}}int xTwo = 0;for (int x : nums) {if (((x >>> k) & 1) == 1) {xTwo ^= x;}}return new int[] {xTwo, xOne ^ xTwo};}
}

更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

转载于:https://www.cnblogs.com/fengsehng/p/6048671.html

【leetcode79】Single Number III相关推荐

  1. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  2. 【leetcode77】Single Number

    一题目描述: 给定一个数组,只有一个数字出现一次,其余都是两次,判断那个数字 思路: 不断取出数据进行异或,最后一个数字,因为相同的数字会抵消 代码: public class Solution {p ...

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

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

  4. leetcode【537】Complex Number Multiplication(复数相乘)

    写在最前面:一道很常规的字符串分割的题 leetcode[537]Complex Number Multiplication Given two strings representing two co ...

  5. 【HDOJ】1005 Number Sequence_天涯浪子_新浪博客

    [题目]http://acm.hdu.edu.cn/showproblem.php?pid=1005 [报告] 这道明显是数学题,但如果按位枚举显然是要TLE的. 仔细思考,它有一个MOD 7,这个很 ...

  6. 【u020】Couple number

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple numb ...

  7. 【Leetcode】Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 思路:若使用[Leetcode]Reverse I ...

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

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

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

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

最新文章

  1. Linux哪个和Windows很像,Linuxfx - 这套Linux操作系统看起来和Windows 10非常类似
  2. ICML 2021 (Long Oral) | 深入研究不平衡回归问题
  3. python为什么那么难学_Python入门很难吗? 为什么越来越多的人都学Python?
  4. Cortex M3寄存器组
  5. HBase伪分布式集群配置
  6. python统计图原理_AB测试原理及样本量计算的Python实现
  7. MapReduce:简单介绍
  8. 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)—— 0.学习目标...
  9. linux的常用操作——程序调试gdb
  10. csapp bufbomb实验
  11. python作用域顺序排列_详解Python函数作用域的LEGB顺序
  12. Web后端学习笔记 Flask(9)cookie and session
  13. 为什么 scrum 开发人员是一个 T-形的人 ?
  14. 音频频谱特效 jaVa_基于Matlab dsp工具箱 的实时音频采集及频谱显示
  15. Android 扫描二维码、制作二维码、识别图片中的二维码;简单的Zxing扫一扫功能;
  16. Chrome OS超便捷安装指南
  17. 方舟手游怎么在服务器用gg修改器,方舟手游gg修改器脚本
  18. 分布式统一框架的设计与实现(数据库)
  19. 应对Flash中国版流氓后台偷窥的解决方法
  20. 解决win10输入法无法使用问题

热门文章

  1. SQL 使用总结五(杂记)
  2. Odoo10教程---模块化三:模型约束,高级视图,工作流,安全性,向导,国际化和报表等
  3. php mysql latin1_mysql从latin1转utf-8的经验
  4. mysql数据库 uft8_unicode_ci是utf8吗_MysQL数据库中utf8_unicode_ci与utf8_general_ci的区别
  5. tp5分页不加载搜索参数
  6. lisp读点坐标绘多义线_AutoCAD中多义线的坐标标注
  7. html 生成唯一码,生成唯一32位ID编码代码,以满足对ID编号的唯一性加资源性解决问题...
  8. ul 原点显示_html ul li在div里圆点靠左对齐
  9. android接口调用超时,RCA:收单设备调用云端接口频繁超时排查总结
  10. matlab画坐标系,Matlab绘制图形坐标轴操作汇总 | 望天博客