leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
解题思路:
如果以线性复杂度和不申请额外内存的标准来衡量这道题,那么还是比较有难度的。
利用位运算之异或的性质来解。
代码如下:
class Solution {
public:
int singleNumber(const vector<int>& nums) const {
int result = ;
for(auto e : nums)
{
result ^= e;
}
return result;
}
};
leetcode 137. Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
解题思路:
利用 unordered_map 模拟 hash 表,记录元素出现过的次数 pair<number, times>
代码如下:
class Solution {
public:
int singleNumber(const vector<int>& nums) {
unordered_map<int, unsigned> m;
for(auto e : nums)
{
m[e]++;
}
for(auto e : m)
{
if(e.second == ) return e.first;
}
return -;
}
};
leetcode 260. 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?
解题思路:
把所有数存入哈希表,数值大小作为 key ,该数出现的次数——value——每次累加。
然后再遍历哈希表,把 value == 1 的 key 取出来即是本题答案。
代码如下:
class Solution {
public:
vector<int> singleNumber(const vector<int>& nums) {
unordered_map<int, unsigned> m;
for(auto e : nums)
{
m[e]++;
}
vector<int> v;
for(auto e : m)
{
if(e.second == )
{
v.push_back(e.first);
}
}
return v;
}
};
leetcode 136 Single Number, 260 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 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 ...
- 136.137.260. Single Number && 位运算
136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...
- LeetCode 136 Single Number(仅仅出现一次的数字)
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...
- [LeetCode] 136. Single Number 单独数
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- 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 ...
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...
随机推荐
- Linux:file命令显示自定义文件类型
file 命令可以查看文件类型信息,原理见: 非常Linux-file命令与magic file 修改 /ect/magic 文件后,可用 file 命令显示自定义文件类型信息. man magic ...
- hive学习-测试数据
测试数据: ---------------------------------------------MySQL-------------------------------------------- ...
- httpd.conf .htaccess apache 服务器配置
PHP Advanced and Object-Oriented Programming Larry Ullman The standard solution in these situations ...
- php 实现 二维码 扫描登录
本人简单实现的示例,使用任意二维码工具打开二维码对应链接 http://www.54php.cn/demo1/scan 原理介绍: 第一步:访问登录页面,生成唯一key,例如MkhjDFL=,并且将此 ...
- 2018/03/09 每日一个Linux命令 之 chgrp/chown
每日一个Linux命令 2018-03-09 Linux 命令 chgrp/chown chgrp [-参数] [文件或者目录] chown [-参数] [文件所有者]:[文件所属群组] [文件或者目 ...
- 用tomcat发布自己的Java项目
作为一名web开发人员,用的又是Java语言,少不得和tomcat打交道.tomcat是什么?它是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器.我们用Java开发出来的web项目,通 ...
- 【Echarts】图表用echarts【待完善】
echarts是做数据统计. ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox ...
- Spark Core(三)Executor上是如何launch task(转载)
1. 启动任务 在前面一篇博客中(Driver 启动.分配.调度Task)介绍了Driver是如何调动.启动任务的,Driver向Executor发送了LaunchTask的消息,Executor接收 ...
- [vue]组件的创建(componet)和销毁(keep-alive缓存)和父子dom同步nextTick
思路: 1. 组件的好处,重用性 2. 组件对的slot用法 3. 子如何调用父的数据 4. 子如何触发父的方法执行 5. 父如何触发子的方法执行 6. 如何创建组件和销毁自建--如何缓存避免每次切换 ...
- [na]timewait优化
解决timewait 加入一条socket配置,重用ip和端口 phone=socket(AF_INET,SOCK_STREAM) phone.setsockopt(SOL_SOCKET,SO_REU ...