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 ...
随机推荐
- matplotlib的下载和安装方法
官网:http://matplotlib.org/ Installation节 Visit the Matplotlib installation instructions. Installing节 ...
- HDU 2089 - 不要62 - [数位DP][入门题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- Oracle管理监控之oracle客户端链接服务器配置文档
开始菜单--Oracle - OraClient10g_home1--配置和移植工具--Net Configuration Assistant 打开窗口如下: 选择 本地Net服务名配置 点 下一步 ...
- eclipse反编译插件jadClipse安装使用教程
previously:最近在学习Dependency Injection(依赖注入)模式,看了 martin fowler 的 文章(原文:https://martinfowler.com/artic ...
- 2018/04/17 每日一个Linux命令 之 tar
10天没有更新这个每日学习 linux 了,因为实在很忙,晚上还要看会其他知识. 但是也不应该给自己找理由,还是应该每天的坚持下去 -- tar 用于在 linux 解压缩/文件 这个命令下面的参数非 ...
- rem布局,flexible.js
//author:caibaojian //website:http://caibaojian.com //weibo:http:weibo.com/kujian //这段js的最后面有两个参数记得要 ...
- Python-OpenCV —— 基本操作详解
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.MacOS操作系统上.它轻量级而且高效——由一系列 C 函数和少量C++类构成,同时提供了Pyt ...
- hibernate注解(三)1+N问题
一.什么时候会遇到1+N的问题? 前提:Hibernate默认表与表的关联方法是fetch="select",不是fetch="join",这都是为了懒加载而准 ...
- 【Pyton】【小甲鱼】类和对象:一些相关的BIF(内置函数)
1.issubclass(class,classinfo) 1)一个类被认为是其自身的子类 2)classinfo可以使类对象组成的元祖,只要class与其中任何一个候选类的子类,则返回True. & ...
- Oracle与Sql server 在SQL上的不同
Oracle与Sql server都遵循SQL-92标准:http://owen.sj.ca.us/rkowen/howto/sql92F.html,但是也有一些不同之处,差别如下: Oracle中表 ...