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:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. 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的更多相关文章

  1. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  2. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  3. [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 ...

  4. 136.137.260. Single Number && 位运算

    136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...

  5. LeetCode 136 Single Number(仅仅出现一次的数字)

    翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...

  6. [LeetCode] 136. Single Number 单独数

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  7. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  8. 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 ...

  9. [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 ...

随机推荐

  1. SHU 413 - 添加好友

    题目链接:http://acmoj.shu.edu.cn/problem/413/ 不难发现,这题是求C(n,1)+C(n,2)+C(n,3)+……+C(n,n-1)+C(n,n) 根据二项展开式有( ...

  2. linux:帮助命令help、man、info

    笔记内容如下: 1.内建命令与外部命令之分2.help , man , info命令的使用以及区别 内建命令与外部命令 有一些查看帮助的工具在内建命令与外建命令上是有区别对待的. 内建命令实际上是 s ...

  3. 源码 time sleep

    C:\Users\Administrator\.PyCharm2017.1\system\python_stubs\2083891348\time.py def sleep(seconds): # r ...

  4. 从url到请求 再到页面生成

    百度面试 从url到请求 再到页面生成 - MartinDing - 博客园 https://www.cnblogs.com/martinding/p/7458723.html

  5. numpy、pandas、scipy介绍

    https://blog.csdn.net/LOLITA0164/article/details/80195124 numpy简介NumPy(Numeric Python)是一个Python包.它是一 ...

  6. Java8 Collectors.toMap的坑

    按照常规思维,往一个map里put一个已经存在的key,会把原有的key对应的value值覆盖,然而通过一次线上问题,发现Java8中的Collectors.toMap反其道而行之,它默认给抛异常,抛 ...

  7. router-link params传参

    1.router.js配置 需要在路径后定义上要传的属性名 -->       /:属性名(query方式不需要) { path: '/CreateProgress/:name1', name: ...

  8. php 中date显示时间不对与Linux文件乱码问题

    php 中date显示时间不对解决办法如下1.修改/etc/php.ini文件 在里头中找到data.timezone =去掉它前面的分号';' 然后设置data.timezone = “Asia/S ...

  9. Linux新建用户 useradd&groupadd

    建立一个新组,并设置组ID加入系统:#groupadd -g 1000 sparkgroup #useradd -u 2000 -g sparkgroup sparkuser #mkdir -p /a ...

  10. windows上apache是线程处理请求,linux上apache是进程处理请求

    windows上apache是线程处理请求,linux上apache是进程处理请求