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 ...
随机推荐
- Mysql 插入数据,随机事件选择
在拼写sql的 时候,mysql字段如果需要添加当前时间可以用NOW() 函数 // String sql = ("insert into tablename(content, create ...
- Activiti学习笔记8 — UserTask私有任务的使用
每一个UserTask都会在Execution表和Task表中各产生一条记录 一.创建流程引擎对象 /** * 1.创建流程引擎对象 */ private ProcessEngine processE ...
- WebApi路由机制详解
随着前后端分离的大热,WebApi在项目中的作用也是越来越重要,由于公司的原因我之前一直没有机会参与前后端分离的项目,但WebApi还是要学的呀,因为这东西确实很有用,可单独部署.与前端和App交互都 ...
- kerberos协议介绍
一.kerberos认证过程: client向kerberos服务请求,希望获取访问server的权限.kerberos得到了这个消息,首先得判断client是否是可信赖的,也就是白名单黑名单的说法. ...
- grunt完整的配置demo
const path = require('path') const fs = require('fs'); module.exports = function (grunt) { grunt.reg ...
- Python全栈开发:django网络框架(一)
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- JavaScript 点击事件的三种写法
嵌入式 <button οnclick='alert("hello")'>点击按钮</button> 脚本模型 btn.onclick=function() ...
- windows cmd command
////////////////// ===>windows + r //gpedit.msc 用户组策略 ///////////////// ===>cmd //ping www.bai ...
- python笔记四
#!/usr/bin/env python3 from datetime import datetime, timedelta # datetime是模块,datetime模块还包含一个datetim ...
- JAVA判断一个对象生存还是死亡
JAVA中判断一个对象是否死亡的算法有两种: 引用计数算法 可达性分析算法 一.引用计数算法所谓引用计数算法就是,给一个对象定义一个引用计数器,每当该对象被引用一次引用计数器就加1,如果一个对象的引用 ...