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. POJ-2181 Jumping Cows(贪心)

    Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Descript ...

  2. kdevelop使用笔记

    https://www.cnblogs.com/-Mr-y/p/7707176.html#_label7

  3. Hive之import和export使用详解

    在hive-0.8.0后引入了import/export命令. Export命令可以导出一张表或分区的数据和元数据信息到一个输出位置,并且导出数据可以被移动到另一个hadoop集群或hive实例,并且 ...

  4. Oracle体系结构之Oracle静态监听配置模板

    1.监听程序配置:[oracle@localhost admin]$ vim listener.ora# listener.ora Network Configuration File: /u01/a ...

  5. django中admin的使用

    转载网址:https://www.cnblogs.com/wumingxiaoyao/p/6928297.html     Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理 ...

  6. Set-cookie无效(失效)

    今天做爬虫的时候遇到网站响应response返回的数据中有Set-Cookie,但是使用Linux的curl请求网页保存cookie始终为空,换句话说也就是Set-Cookie设置无效,所以我一直Go ...

  7. 网页设计师必知的10则SEO

    如今,Web设计师在设计Web外观与风格的同时,往往还负责了前端代码的编写.换???话说,SEO的相当一部分责任是落在他们肩上的.然而,大 量的 Web设计师对SEO的熟悉程度仍不足以让他们写出一个符 ...

  8. Cable master---poj1064(二分|卡精度)

    题目链接:http://poj.org/problem?id=1064 题意:有n条绳子,长度为Li,现在从这n条绳子中切割出K条相等的绳子,问切割出来的这k条绳子的最大长度为多少: 二分判断即可: ...

  9. 2015 湘潭大学程序设计比赛(Internet)--E题--烦人的异或

    烦人的异或 Accepted : 27   Submit : 102 Time Limit : 5000 MS   Memory Limit : 65536 KB 题目描述 如下图,有一N*M的表格, ...

  10. easyUi引入方法

    1:创建一个动态web工程:    去官网http://www.jeasyui.net/download/下载官网文档    我去官网下载的最新版本,个人根据自己的需求下载即可.2:在webConte ...