【leetcode79】Single Number III
题目描述:
给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出
原文描述:
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专栏,链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode79】Single Number III的更多相关文章
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【03_136】Single Number
感谢:http://www.cnblogs.com/changchengxiao/p/3413294.html Single Number Total Accepted: 103007 Total S ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【Leetcode】 - Single Number II
Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...
- 【Leetcode】【Medium】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode】Single Number (Medium) ☆
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
随机推荐
- MYSQL 表左连接 ON AND 和ON WHERE 的区别
首先是针对左右连接,这里与inner join区分 在使用left join时,on and 和on where会有区别 1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条 ...
- 天梯赛-L1-018. 大笨钟
L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...
- 03 持续集成和部署/基础设施 - DevOps之路
02 持续集成和部署/基础设施 - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi 服务的持续集成和部署这里有两套 ...
- vue+node.js+webpack开发微信公众号功能填坑——v -for循环
页面整体框架实现,实现小功能,循环出数据,整体代码是上一篇 vue+node.js+webpack开发微信公众号功能填坑--组件按需引入 修改部门代码 app.vue <yd-flexbox&g ...
- <心得小记>2015年10月3日 14:16:42
急事,慢慢说:大事,清楚的说: 小事,幽默的说了:没把握的事,谨慎的说: 没发生的事,不要胡说:做不到的事,别乱说: 伤害人的事,不能说:讨厌的事,对事不对人的说: 开心的事,看场合说:伤心的事,不要 ...
- PHP 安装
PHP 安装 您需要做什么? 为了开始使用 PHP,您可以: 找一个支持 PHP 和 MySQL 的 Web 主机 在您自己的 PC 机上安装 Web 服务器,然后安装 PHP 和 MySQL 使用支 ...
- 计算机网络之动态主机配置协议DHCP
为了将软件协议做成通用的和便于移植,协议软件的编写者不会把所有细节都固定在源代码中,而是把协议软件参数化,这就使得在很多台计算机上使用同一个经过编译的二进制代码成为可能. 一台计算机和另一台计算机的区 ...
- (译)Objective-C 类属性
翻译自:Objective-C Class Properties 译者:Haley_Wong 由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.苹果展 ...
- 一个貌似比较吊的递归转换为loop--总算成功了.
class Stack(object): """ A class to hold arguements and state data. """ ...
- Dynamics CRM2013 从subgrid中打开快速创建窗体创建数据
在页面上使用subgrid时,在subgrid中新建数据时需要跳转到另一个页面,这种操作比较麻烦且很不友好,这时我们想到了快速创建窗体,像下图这样直接在当前页上方下拉出现一个窗体,填写内容后点击保存就 ...