LeeCode-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?
题目很简单,但是通过这个题目让我见识了LeetCode众多大牛的存在。
代码中,第一次了使用accumulate函数,然后还要bit_xor<int>()之类的C++11的function object。
此处如果diff = INT_MIN, 那么 -diff 恰好会溢出到 INT_MIN. 之前并没有注意过这个问题。
- class Solution {
- public:
- vector<int> singleNumber(vector<int>& nums) {
- int diff = accumulate(begin(nums), end(nums), , bit_xor<int>());
- diff &= -diff;
- vector<int> result = {, };
- for (auto num: nums) {
- result[!(diff & num)] ^= num;
- }
- return result;
- }
- };
LeeCode-Single Number III的更多相关文章
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 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 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number Given an array of integers, every element appears twice except for one. Find that ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [LeetCode] Single Number III ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Spring Cloud Feign设计原理
什么是Feign? Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直 ...
- uoj140 【UER #4】被粉碎的数字
题目 看起来就像是数位\(\rm dp\) 不妨从竖式乘法的角度来考虑这个问题 为了方便处理进位,我们得从低位向高位填数 设\(dp[i][0/1][j][p][t]\)表示填到了第\(i\)位,卡不 ...
- docker使用gitlab持续集成(1)
修改ssh连接端口vi /etc/ssh/sshd_config 写docker-compose.yml文件配置gitlab version: '3' services: gitlab: image: ...
- mysql连接数问题备份
一. max_connections 这是是查询数据库当前设置的最大连接数 mysql> show variables like '%max_connections%';+----------- ...
- UC浏览器禁止图片阅读模式处理方法
本文转载自:https://www.cnblogs.com/MY0101/p/9969818.html UC浏览器点击图片会出现图片阅读模式. 如何处理? <img style=" w ...
- Jquery中attr与prop的区别详解
prop()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性,返回值是空字符串. attr()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性, ...
- Windows 隐藏控制台
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"& ...
- CSS3——动画
动画是CSS3中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果. 语法格式: animation:动画名称 动画时间 运动曲线 何时开始 播放次数 是否反 ...
- thinkphp DEFINED标签
DEFINED标签用于判断某个常量是否有定义,用法如下: 大理石平台检验标准 <defined name="NAME"> NAME常量已经定义 </defined ...
- 如何查看redis占用内存大小
redis缓存固然高效,可是它会占用我们系统中宝贵的内存资源,特别是当我们的项目运行了一段时间后,我们需要看一下redis占用了多少内存,那么可以用“info”命令查看. 执行info命令后,找到Me ...