JM233333's Blog
  • Programming Languages

    • C
    • UNIX-C
    • Python
  • Algorithms and Data Structures

    • Data Structure
    • Fundamental Algorithms
    • Graph Theory
  • GNU Toolchain

    • Bash
    • gdb
  • Development Environment

    • Ubuntu
    • QEMU
  • Development Tools

    • Git
    • VSCode
  • Operating Systems

    • Principles of Operating Systems
    • Xv6
    • Linux Kernel
  • Software Testing and Analysis

    • Software Testing
    • Software Analysis
    • Program Verification
  • LeetCode
  • XJTUOJ
  • Programming

    • Is Parallel Programming Hard
  • System

    • System Performance
  • Others

    • ...
  • Paper Reading

    • Model Checking
    • Fuzzing
    • Symbolic Execution
  • 3D Game Programming

    • 3D Mathematics
  • Miscellaneous

JM233333

弱小可怜又无助的学术废物
  • Programming Languages

    • C
    • UNIX-C
    • Python
  • Algorithms and Data Structures

    • Data Structure
    • Fundamental Algorithms
    • Graph Theory
  • GNU Toolchain

    • Bash
    • gdb
  • Development Environment

    • Ubuntu
    • QEMU
  • Development Tools

    • Git
    • VSCode
  • Operating Systems

    • Principles of Operating Systems
    • Xv6
    • Linux Kernel
  • Software Testing and Analysis

    • Software Testing
    • Software Analysis
    • Program Verification
  • LeetCode
  • XJTUOJ
  • Programming

    • Is Parallel Programming Hard
  • System

    • System Performance
  • Others

    • ...
  • Paper Reading

    • Model Checking
    • Fuzzing
    • Symbolic Execution
  • 3D Game Programming

    • 3D Mathematics
  • Miscellaneous
  • LeetCode
  • LeetCode 0001 - 0050

    • LeetCode 0001 - Two Sum
      • Hints
      • 题面
      • 题意
      • 题解
      • AC代码
    • LeetCode 0002 - Add Two Numbers
    • LeetCode 0003 - Longest Substring Without Repeating Characters
    • LeetCode 0004 - Median of Two Sorted Arrays
    • LeetCode 0005 - Longest Palindromic Substring
    • LeetCode 0006 - ZigZag Conversion
    • LeetCode 0007 - Reverse Integer
    • LeetCode 0008 - String to Integer (atoi)
    • LeetCode 0009 - Palindrome Number
    • LeetCode 0010 - Regular Expression Matching
    • LeetCode 0011 - Container With Most Water
    • LeetCode 0012 - Integer to Roman
    • LeetCode 0013 - Roman to Integer
    • LeetCode 0014 - Longest Common Prefix
    • LeetCode 0015 - 3Sum
    • LeetCode 0016 - 3Sum Closest
    • LeetCode 0017 - Letter Combinations of a Phone Number
    • LeetCode 0018 - 4Sum
    • LeetCode 0019 - Remove Nth Node From End of List
    • LeetCode 0020 - Valid Parentheses
    • LeetCode 0021 - Merge Two Sorted Lists
    • LeetCode 0022 - Generate Parentheses
    • LeetCode 0023 - Merge k Sorted Lists
    • LeetCode 0024 - Swap Nodes in Pairs
    • LeetCode 0025 - Reverse Nodes in k-Group
    • LeetCode 0026 - Remove Duplicates from Sorted Array
    • LeetCode 0027 - Remove Element
    • LeetCode 0028 - Implement strStr()
    • LeetCode 0029 - Divide Two Integers
    • LeetCode 0030 - Substring with Concatenation of All Words
    • LeetCode 0031 - Next Permutation
    • LeetCode 0032 - Longest Valid Parentheses
    • LeetCode 0033 - Search in Rotated Sorted Array
    • LeetCode 0034 - Find First and Last Position of Element in Sorted Array
    • LeetCode 0035 - Search Insert Position
    • LeetCode 0036 - Valid Sudoku
    • LeetCode 0037 - Sudoku Solver
    • LeetCode 0038 - Count and Say
    • LeetCode 0039 - Combination Sum
    • LeetCode 0040 - Combination Sum II
    • LeetCode 0041 - First Missing Positive
    • LeetCode 0042 - Trapping Rain Water
    • LeetCode 0043 - Multiply Strings
    • LeetCode 0044 - Wildcard Matching
    • LeetCode 0045 - Jump Game II
    • LeetCode 0046 - Permutations
    • LeetCode 0047 - Permutations II
    • LeetCode 0048 - Rotate Image
    • LeetCode 0049 - Group Anagrams
    • LeetCode 0050 - Pow(x, n)
  • LeetCode 0051 - 0100

  • LeetCode others

  • LeetCode 0001 - xxx
  • leetcode
  • LeetCode 0001 - 0050
JM233333
2019-11-01
606

LeetCode 0001 - Two Sum

Creative Commons

# Hints

  • 水题

# 题面

Difficulty Time Complexity Limit Extra-Memory Complexity Limit
Easy O(n)O(n)O(n) −-−

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

# 题意

给定一个长为 nnn 的数组,给定一个整数 targettargettarget ,求出一个二元组 (i,j),i≠j(i,\ j),\ i \ne j(i, j), i​=j ,满足 numsi+numsj=targetnums_i + nums_j = targetnumsi​+numsj​=target 。如果有多组解,求出任意一组解即可。


# 题解

暴力枚举的做法很显然,时间复杂度为 O(n2)O(n^2)O(n2) 。也容易想到枚举其中一个数然后二分查找另一个数(当然,需要先排序),时间复杂度为 O(nlog⁡n)O(n\log{n})O(nlogn) 。这两种方法都非常简单,此处不予赘述。

我们维护一个哈希表或者unordered_map,记录数组中每个数值对应的下标,初始为空。我们依次遍历数组中的每个元素,查找表中此时是否有键为 target−numitarget - num_itarget−numi​ 的记录,如果找到则返回答案,否则将 numinum_inumi​ 插入表中。而且问题只需要求出任意一组解,我们甚至不需要考虑哈希冲突的问题。

因为unordered_map是 O(1)O(1)O(1) 操作的,所以总的时间复杂度为 O(n)O(n)O(n) 。


# AC代码

class Solution {
public:
	vector<int> twoSum(vector<int> & nums, int target) {
		// 求解
		unordered_map<int, int> ump;
		for (int i = 0; i < nums.size(); i ++) {
			// 查找
			int val = target - nums[i];
			if (ump.find(val) != ump.end()) {
				return vector<int>({ump[val], i});
			}
			// 插入
			ump[nums[i]] = i;
		}
		// 返回
		return vector<int>({-1, -1});
	}
};

#LeetCode#LeetCode Easy#水题

← LeetCode LeetCode 0002 - Add Two Numbers→

最近更新
01
Reading Papers - Kernel Concurrency
06-01
02
Linux Kernel - Source Code Overview
05-01
03
Linux Kernel - Per-CPU Storage
05-01
更多文章>
Theme by Vdoing | Copyright © 2019-2023 JM233333 | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式