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 ...
随机推荐
- Minimalist GNU for Windows
MinGW | Minimalist GNU for Windows http://www.mingw.org/ MinGW, a contraction of "Minimalist GN ...
- address sizes : 46 bits physical, 48 bits virtual
processor : 7vendor_id : GenuineIntelcpu family : 6model : 63model name : Intel(R) Xeon(R) CPU E5-26 ...
- 特征向量-Eigenvalues_and_eigenvectors#Graphs 线性变换
总结: 1.线性变换运算封闭,加法和乘法 2.特征向量经过线性变换后方向不变 https://en.wikipedia.org/wiki/Linear_map Examples of linear t ...
- Linux下缓冲区溢出攻击的原理及对策
前言 从逻辑上讲进程的堆栈是由多个堆栈帧构成的,其中每个堆栈帧都对应一个函数调用.当函数调用发生时,新的堆栈 帧被压入堆栈:当函数返回时,相应的堆栈帧从堆栈中弹出.尽管堆栈帧结构的引入为在高级语言中实 ...
- Metasploit services
漏洞挖掘/漏洞分析-- Cve.mitre.org www.corelan.be----geek of pentesters http://wrox.cn/article/100048133/ ...
- mysql 权限管理 针对表的字段 级别 授权 columns_priv表
针对Mike账号 db1库下面的t1表的 id,name字段授予select权限,age字段授予update权限 授权格式 select(要授权的字段,要授权的字段) 用户括号 括起来 .updat ...
- 【2014腾讯实习招聘-面试-移动client开发】
版权声明:本文为博主原创文章,欢迎转载,转载请注明出处. https://blog.csdn.net/kana007/article/details/24375423 个人学习整理.如 ...
- 006-spring cloud gateway-GatewayAutoConfiguration核心配置-GatewayProperties初始化加载、Route初始化加载
一.GatewayProperties 1.1.在GatewayAutoConfiguration中加载 在Spring-Cloud-Gateway初始化时,同时GatewayAutoConfigur ...
- 教你在Android手机上使用全局代理
前言:在Android上使用系统自带的代理,限制灰常大,仅支持系统自带的浏览器.这样像QQ.飞信.微博等这些单独的App都不能使用系统的代理.如何让所有软件都能正常代理呢?ProxyDroid这个软件 ...
- ffmpeg综合应用示例(三)——安卓手机摄像头编码
本文的示例将实现:读取安卓手机摄像头数据并使用H.264编码格式实时编码保存为flv文件.示例包含了 1.编译适用于安卓平台的ffmpeg库 2.在java中通过JNI使用ffmpeg 3.读取安卓摄 ...