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. codeforces#514 Div2---1059ABCD

    1059A---Cashier http://codeforces.com/contest/1059/problem/A 题意: Vasya每天工作\(l\)个小时,每天服务\(n\)个顾客,每个休息 ...

  2. linux下pip安装pygame

    在学习python的过程中要用到pygame,在安装过程中遇到一些问题,经百度解决.因为使用的版本为python3,故以下教程针对python3版本.安装教程如下: 一.首先你要确保你已经安装了pip ...

  3. AjaxAnywhere的用法(FORWARD)

    AjaxAnywhere的用法   ajaxanywhere 总结:1,简介AjaxAnywhere被设计成能够把任何一套现存的JSP组件转换成AJAX感知组件而不需要复杂的JavaScript编码. ...

  4. HDFS Snapshots

    Overview HDFS Snapshots are read-only point-in-time copies of the file system. Snapshots can be take ...

  5. 启用yarn日志聚集功能

    在yarn-site.xml配置文件中添加如下内容: ##开启日志聚集功能        <property>                <name>yarn.log-ag ...

  6. 判断tableViewCell是否在可视区

    1.- (NSArray *)visibleCells; UITableview 的方法,这个最直接,返回一个UITableviewcell的数组. 对于自定义的cell,之后的处理可能会稍微复杂. ...

  7. iOS多线程编程之GCD介绍(转载)

    一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 GCD是苹果公司为多核的并行运算提 ...

  8. hive-site.xml配置

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-s ...

  9. 前端 html input标签 placeholder属性 标签上显示内容

    前端 html  input标签 的placeholder属性  标签上显示内容 <!DOCTYPE html> <html lang="en"> < ...

  10. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...