LeetCode Notebook 2020-2021
  • 序
  • 目录
  • 线性表
    • 数组
      • Remove Duplicates from Sorted Array
      • Remove Duplicates from Sorted Array II
      • Exercises I
      • Search in Rotated Sorted Array
      • Search in Rotated Sorted Array II
      • Median of Two Sorted Arrays
      • Longest Consecutive Sequence
      • Exercises II
      • Two Sum
      • 3Sum
      • 3Sum Closest
      • 4Sum
      • Exercises III
      • Next Permutation
      • Permutation Sequence
      • Exercises IV
      • Valid Sudoku
      • Trapping Rain Water
      • Rotate Image
      • Plus One
      • Exercises V
      • Climbing Stairs
      • Set Matrix Zeroes
      • Gray Code
      • Gas Station
      • Exercises VI
      • Candy
      • Single Number
      • Single Number II
      • Exercises VII
    • 链表
      • Add Two Numbers
      • Reverse Linked List II
      • Partition List
      • Swap-nodes-in-pairs
  • 字符串
    • 字符串
      • Valid Palindrome
      • Implement strStr()
      • String to Integer(atoi)
      • Add Binary
      • Longest Palindromic Substring
      • Longest Common Subsequence
      • Regular Expression Matching
      • Rearrange Spaces Between Words
      • Longest Common Prefix
      • Wildcard Matching
      • subdomain visit count
  • 栈和队列
    • 栈
      • Min Stack
      • Valid Parentheses
    • 队列
  • 树
    • 二叉树的遍历
      • Binary Tree Preorder Traversal
      • Binary Tree Inorder Traversal
      • Binary Tree Postorder Traversal
      • Search in a Binary Search Tree
    • 二叉树的构造
      • Untitled
    • 二叉查找树
      • Validate Binary Search Tree
    • 二叉树的递归
      • Untitled
  • 图
    • 图
  • 排序
    • 排序
      • Merge Sorted Array
      • Merge Two Sorted Lists
      • Merge k Sorted Lists
      • Exercises I
      • Insertion Sort List
      • Sort List
      • Sort Color
      • First Missing Positive
      • Exercises II
  • 查找
    • 查找
      • Random Pick with Weight
      • Prefix and Suffix Search
      • Search Insert Position
      • Search a 2D Matrix
  • BFS
    • BFS
  • DFS
    • DFS
      • Split a String Into the Max Number of Unique Substrings
      • Palindrome Partitioning
      • Unique Path
      • Unique Path II
  • 动态规划
    • DP
      • Coin Change
      • Maximal Rectangle
      • Super Egg Drop
      • Exercises
      • Best Team With No Conflicts
  • 分治法
    • Divide And Conquer
  • 贪心法
    • Greedy
      • Jump Game
      • Jump Game II
      • Best time to buy and sell stock
      • Best time to buy and sell stock II
      • Container with most water
      • Exercises
  • 暴力枚举
    • Brute Force
      • Subsets
      • Subsets II
      • Permutation
      • Permutation II
      • Combinations
      • Letter Combinations of a Phone Number
  • others
    • 小岛题
    • 滑动窗口
      • Longest substring without repeating characters
      • Sliding Window Maximum
      • Max Consecutive One III
      • Count Number of Nice Subarrays
    • Roman number code
    • Untitled
由 GitBook 提供支持
在本页
  • Single Number
  • 描述
  • 例子
  • 解法 快慢指针 answer wrong 6/16
  • 解法 HashTable
  • 解法 math
  • 解法binary

这有帮助吗?

  1. 线性表
  2. 数组

Single Number

easy 原题链接:https://leetcode.com/problems/single-number/

上一页Candy下一页Single Number II

最后更新于4年前

这有帮助吗?

Single Number

描述

Given a non-empty 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?

例子

Input: [2,2,1]

Output: 1

解法 快慢指针 answer wrong 6/16

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res=0, fastres=1;
        while(res<fastres&&fastres<nums.size()){
            if(nums[res]==nums[fastres]){
                res=res+2;
                fastres=res+1;
            }else{fastres++;}
        }
        return nums[res];
    }
};

解法 HashTable

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        unordered_map<int, int> hash_table;
        for(int i:nums){
            hash_table[i]++;
        }
        for(auto m:hash_table){
            if(m.second==1){
                return m.first;
            }
        }
        return 0;
    }
};

解法 math

2∗(a+b+c)−(a+a+b+b+c)=c

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int sumOfset=0, sumOfNums=0;
        unordered_set<int> seen;
        for(int num: nums){
            if(!seen.count(num)){
                seen.insert(num);
                sumOfset+=num;
            }
            sumOfNums+=num;
        }
        return 2*sumOfset-sumOfNums;
    }
};

解法binary

Concept

  • If we take XOR of zero and some bit, it will return that bit

    • a ⊕0 = a

  • If we take XOR of two same bits, it will return 0

    • a⊕a=0

  • a⊕b⊕a=(a⊕a)⊕b=0⊕b=b

So we can XOR all bits together to find the unique number.

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int a=0;
        for(int i:nums){
            a ^= i;
        }
        return a;
    }
};
原题链接