【一天一道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 ...
随机推荐
- NOIP2016 玩脱记
NOIP前: NOIP前停课了一个多月,这一个多月里浪得飞起,内心十分紧张,然后就不知不觉就到NOIP了. Day 0: 上火车前ryc给我们出了道题"一个数列,只有两个数出现了奇数次,找出 ...
- 第三次C语言作业
(一)改错题 计算f(x)的值:输入实数x,计算并输出下列分段函数f(x)的值,输出时保留1位小数. 输入输出样例1: Enterr x: 10.0 f(10.0) = 0.1 输入输出样例2: En ...
- Optaplanner逐步学习(0) : 基本概念 - Optaplanner,规划问题, 约束,方案
之前的文章中,分别从APS,排产到规划引擎叙述了一些理论基础:并介绍了一些Optaplanner大概的情况:并一步步将Optaplanner的示例运行起来,将示例源码导进Eclipse分析了一下它的H ...
- SQL之DISTINCT
警告:不能部分使用DISTINCT. DISTINCT关键字作用于所有的列,不仅仅是跟在其后的那一列.例如,你指定SELECT DISTINCT vend_id, prod_price,除非指定的两列 ...
- monitoring with Prometheus
Prometheus是一款开源的监控工具,支持k8s metrics的数据格式,同时也支持通过k8s api进行服务发现从而实现对自定义的metrics进行监控.下面通过一个示例来介绍如何将Prome ...
- 360搜索引擎so自动收录php改写方案——适合phpcms等cms
360搜索引擎自动收录功能,官方提供了代码,带式,十分坑爹,没有提供批量提交入口,只是提供了一段js代码,关键是 一个js去下载另外一个js,document.write到文档,然后再 重复2遍如此工 ...
- web缓存之--http缓存机制
一.web缓存可以分为数据库缓存.代理服务器缓存.浏览器缓存. 其中浏览器缓存又包含很多内容:http缓存.indexDb.cookie.localStorage等.本片只讨论http缓存相关内容. ...
- c++银行家算法
#include <iostream> #include<string> #define False 0 #define True 1 using namespace std; ...
- 解析配置文件redis.conf
units单位: # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 ...
- COCO 数据集的使用
Windows 10 编译 Pycocotools 踩坑记 COCO数据库简介 微软发布的COCO数据库, 除了图片以外还提供物体检测, 分割(segmentation)和对图像的语义文本描述信息. ...