【一天一道LeetCode】#260. Single Number III
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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?
(二)解题
题目大意:在一个数组中有两个数只出现过一次,其他的数都出现两次,找出这两个数
解题思路:对比【一天一道LeetCode】#137. Single Number,如果有两个只出现一次的数,那么用异或运算可以找出这两个数的异或值
从这个异或值下手,这两个数不同,那么就可以找出其中不同的位,取一个不同的位,如第M位。
将整个数组分为两类,一类为第M位为1,一类为第M类为0,这样在每一组数中只有一个只出现一次的数,很容易就用异或运算来解决了。
具体思路见代码:
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
vector<int> ret;
int size = nums.size();
if(size == 0) return ret;
int temp = nums[0];
for(int i = 1 ; i < size ;i++) temp^=nums[i];//求全部异或后的最终值
unsigned int bit;
for(int i = 0 ; i < 32 ; i++) {//找到temp中第一个为1的位
bit= 1<<i;
if((temp&bit)==bit) break;
}
int temp1 =0 , temp2 =0;//temp1为第一个只出现一次的数,temp2为第二个
for(int i = 0 ; i < size ;i++){
if((nums[i]&bit)==0) {//分组,第M位上为0的组
temp1^=nums[i];
}
else{
temp2^=nums[i];//第M位上为1的组
}
}
ret.push_back(temp1);
ret.push_back(temp2);
return ret;
}
};
相关题目:
【一天一道LeetCode】#137. Single Number II
【一天一道LeetCode】#137. Single Number
【一天一道LeetCode】#260. Single Number III的更多相关文章
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- [LeetCode] 260. Single Number III 单独数 III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode#260]Single Number III
Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...
- Java [Leetcode 260]Single Number III
题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- Leetcode 260 Single Number III 亦或
在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...
- [LeetCode] 260. Single Number III(位操作)
传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
随机推荐
- RAC基本原理
RAC基本原理 什么是RAC? 多个实例跑在多个服务器上 一个数据库存放在共享的存储上,所有实例都可以访问 实例之间通过内联网络交换数据和信息 共享存储内容:数据文件.REDO.UNDO.控制文件 参 ...
- Tensorflow 基于分层注意网络的文件分类器
After the exercise of building convolutional, RNN, sentence level attention RNN, finally I have come ...
- curl支持HTTP和https
设计流程 基于curl工具实现https/http,设计初步流程为:linux平台验证→→交叉移植arm板. linux系统下调试http和https 1.1 Linux安装curl 输入命令:sud ...
- CentOS7 下安装 Java 8 [wget]
1. 创建一个文件夹 sudo mkdir /usr/local/services/java8 2. 使用 wget 来下载 wget --no-cookies --no-check-certific ...
- HashSet与TreeSet
1.TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不允许放入null值 2.HashSet 是哈希表实现的,HashSet中的数据是无序的,可以放入null,但只能放入一个nu ...
- IOS WebViewJavascriptBridge 使用以及原理分析
本文转自:https://www.jianshu.com/p/b8d4285395c6 概述 从两个方面来讲: js不能直接调用oc的方法 oc可以通过如下函数调用js代码 - (void)evalu ...
- 使用webpack-dev-server设置反向代理解决前端跨域问题
webpack-dev-server是一个小型的Node.js Express服务器,它使用webpack-dev-middleware来服务于webpack的包,除此自外,它还有一个通过Sock.j ...
- spark on yarn 运行问题记录
问题一: 18/03/15 07:59:23 INFO yarn.Client: client token: N/A diagnostics: Application application_1521 ...
- Java 读取Excel文件
https://www.cnblogs.com/wwzyy/p/5962076.html 先把上面的参考博客看了,如果会导入包的话,下面的教程就直接忽略emm 这时候,你应该把jar包下载 ...
- PHP Misc. 函数
PHP 杂项函数简介 我们把不属于其他类别的函数归纳到杂项函数类别. 安装 杂项函数是 PHP 核心的组成部分.无需安装即可使用这些函数. Runtime 配置 杂项函数的行为受 php.ini 文件 ...