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 ...
随机推荐
- POJ-1191-棋盘分割(动态规划)
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13593 Accepted: 4846 Description 将一个 ...
- 【转】C#中的Stream
C# 温故而知新:Stream篇(—) C# 温故而知新:Stream篇(二) C# 温故而知新:Stream篇(三) C# 温故而知新:Stream篇 (四) C# 温故而知新:Stream篇(五) ...
- 【紫书】Undraw the Trees UVA - 10562 递归,字符串
题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...
- Oracle安全之Oracle日志挖掘
logminer基于包: [oracle@localhost ~]$ ls /u01/oracle/10g/rdbms/admin/dbmslm.sql -->dbms_logmnr工具 /u0 ...
- MyISAM存储引擎
每个MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型..frm文件存储表定义.数据文件的扩展名为.MYD (MYData).索引文件的扩展名是.MYI (MYIn ...
- Python的浅拷贝与深拷贝
定义: =号浅拷贝:在Python中对象的赋值其实就是对象的引用.copy了之后两个仍然是同一个东西.那么他们内部的元素自然也是一样的,对其中一个进行修改,另一个也会跟着变> copy()浅拷贝 ...
- upower xdisplay--nvidia -vga---cpu info
grep 'physical id' /proc/cpuinfo | sort -u | wc -l grep 'core id' /proc/cpuinfo | sort -u | wc -l gr ...
- msc文件
MSC微软管理控制台(Microsoft Management Control)文件.可以点击开始/运行,然后输入下列文件名就可以打开相应的控制窗口. 除第三个文件外,其他均在C:\WINDOWS\s ...
- git push 文件过大时出错,fatal: The remote end hung up unexpectedly
可以修改配置文件: 1 使用命令:git config http.postBuffer = 524288000 2修改git文件夹中的config文件,加入如下一段: [http] postBuffe ...
- dedecms提取某栏目及子栏目名称到首页怎么弄
我们建网站时有不同的需求,例如为页面创建一个栏目导航,用dedecms如何提取某栏目及子栏目名称和链接呢?如下图所示,先列出指定的顶级栏目,在下方再列出此栏目的所有子栏目. 之前ytkah说过dede ...