【一天一道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 ...
随机推荐
- 文本分类学习 (五) 机器学习SVM的前奏-特征提取(卡方检验续集)
前言: 上一篇比较详细的介绍了卡方检验和卡方分布.这篇我们就实际操刀,找到一些训练集,正所谓纸上得来终觉浅,绝知此事要躬行.然而我在躬行的时候,发现了卡方检验对于文本分类来说应该把公式再变形一般,那样 ...
- spring 自定义事件发布及监听(简单实例)
前言: Spring的AppilcaitionContext能够发布事件和注册相对应的事件监听器,因此,它有一套完整的事件发布和监听机制. 流程分析: 在一个完整的事件体系中,除了事件和监听器以外,还 ...
- log4j日志的基本使用方法(1)——概述、配置文件
一.概述 Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息的输出格式.日志信息的优先级从高到低有ERROR.WARN.INFO.DEBUG,分别用来指定这条日志信息的重 ...
- rhel7 配置普通用户使用sudo
rhel服务器版本安装之后,默认创建的用户不能使用sudo.使用sudo,会提示 user1 is not in the sudoers file. This incident will be rep ...
- delphi 面向对象实用技能教学一(递归)
本例使用类与TList相结合,用简洁的方法,实现了一个 HTML 解析与格式化功能.所用到的知识点如下:1.类的提前申明2.TList用法3.String的指针操作4.单例设计5.递归用法 编程是综合 ...
- Docker常见仓库Ubuntu
Ubuntu 基本信息 Ubuntu 是流行的 Linux 发行版,其自带软件版本往往较新一些. 该仓库提供了 Ubuntu从12.04 ~ 14.10 各个版本的镜像. 使用方法 默认会启动一个最小 ...
- GDAL打开mdb文件失败解决方法
使用GDAL打开mdb文件时提示下面错误信息: ERROR 1: Unable to initialize ODBC connection to DSN for DRIVER=Microsoft Ac ...
- Spring声明式事务总结
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- Java Web前端到后台常用框架介绍
一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 Spring Web MVC是一种基于Java的实现了Web MV ...
- Leetcode难度表及解题汇总
Leetcode难度表及解题汇总 参考网上一份题目难度表,以及本人的解题. Id Question Difficulty Frequency Data Structures Algorithms Bl ...