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 1984 - Navigation Nightmare - [带权并查集]

    题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...

  2. codeforces 761D - Dasha and Very Difficult Problem

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. sklearn学习_01

    # -*- coding: utf-8 -*- """ Created on Fri Sep 29 11:05:52 2017 机器学习之sklearn @author: ...

  4. Codeforces Round #435 (Div. 2)

    A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...

  5. Myers–Briggs_Type_Indicator 迈尔斯布里格斯类型指标(MBTI)

    Myers–Briggs Type Indicator - Wikipedia https://en.wikipedia.org/wiki/Myers%E2%80%93Briggs_Type_Indi ...

  6. MTU-TCP/IP协议栈-linux kernel-TCP丢包重传-UDP高性能-AI- ip数据报 tcp数据报

    1.IP协议首部 TCP报文段的首部  UDP分组结构   ip数据报 tcp数据报 UDP校验 w 报文长度该字段指定UDP报头和数据总共占用的长度.可能的最小长度是8字节,因为UDP报头已经占用了 ...

  7. AOP 详解

    1. 需求:统计方法执行的性能情况(来源:<精通Spring 4.x>) // 性能监视类 PerformanceMonitor package com.noodles.proxy; pu ...

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

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

  9. javaScript设计模式(一)观察者模式

    哈哈..写了一个钟,一点一点加功能. 1 function Publisher(){ this.subscribers = []; //存储订阅者 this.news = []; //存储要发布的消息 ...

  10. Java8新特性 集合的stream的map

    看该段代码(作用是把List中的对象替换): List<BlackMac> blackMacList = blackMacDao.queryBlackByMac(mac, (paginat ...