【一天一道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 ...
随机推荐
- TDMA over WiFi
0 引言 TDMA可以修正WiFi中DCF机制中连接速率不同终端间信道占用时间片公平性缺陷,从而提升整体WiFi网络的性能.著名的UBNT的网桥就用其独创的TDMA技术为其赢得了市场.以前是不同的公司 ...
- 容器化分布式日志组件ExceptionLess的Angular前端UI
写在前面 随着微服务架构的流行,日志也需要由专门的分布式日志组件来完成这个工作,我们项目使用的是 ExceptionLess 这个组件,它是前后端分离的:这篇文章我们就来实践容器化 Exception ...
- C++ 二分法求解方程的解
二分法是一种求解方程近似根的方法.对于一个函数 f(x)f(x),使用二分法求 f(x)f(x) 近似解的时候,我们先设定一个迭代区间(在这个题目上,我们之后给出了的两个初值决定的区间 [-20,20 ...
- Hinton's paper Dynamic Routing Between Capsules 的 Tensorflow , Keras ,Pytorch实现
Tensorflow 实现 A Tensorflow implementation of CapsNet(Capsules Net) in Hinton's paper Dynamic Routing ...
- salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型
看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...
- Linux学习之CentOS(九)-----文件与目录的默认权限与隐藏权限
文件与目录的默认权限与隐藏权限 一个文件有若干个属性, 包括读写运行(r, w, x)等基本权限,及是否为目录 (d) 与文件 (-) 或者是连结档 (l) 等等的属性! 要修改属性的方法在前面也约略 ...
- $.messager.confirm 用法
<script type="text/javascript"> $(function () { $.messager.defaults = { ...
- day06 Request Response
rw 读写模板的设置 day05 Request Response 1. HttpServletResponse 简介 1.1 Response 的 OutputStream 输出中文的问题 1.2 ...
- Lintcode393 Best Time to Buy and Sell Stock IV solution 题解
[题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...
- JavaScript正则表达式模式匹配(4)——使用exec返回数组、捕获性分组和非捕获性分组、嵌套分组
使用exec返回数组 var pattern=/^[a-z]+\s[0-9]{4}$/; var str='google 2012'; alert(pattern.exec(str)); //返回一个 ...