|
| 1 | +# [1. 两数之和](https://leetcode.com/problems/two-sum) |
| 2 | + |
| 3 | +🟢 <font color=#15bd66>Easy</font>  🔖  [`数组`](/leetcode/outline/tag/array.md) [`哈希表`](/leetcode/outline/tag/hash-table.md)  🔗 [`LeetCode`](https://leetcode.com/problems/two-sum) |
| 4 | + |
| 5 | + |
| 6 | +## 题目 |
| 7 | + |
| 8 | +Given an array of integers `nums` and an integer `target`, return _indices of |
| 9 | +the two numbers such that they add up to`target`_. |
| 10 | + |
| 11 | +You may assume that each input would have **_exactly_ one solution**, and you |
| 12 | +may not use the _same_ element twice. |
| 13 | + |
| 14 | +You can return the answer in any order. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +> Input: nums = [2,7,11,15], target = 9 |
| 21 | +> |
| 22 | +> Output: [0,1] |
| 23 | +> |
| 24 | +> Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. |
| 25 | +
|
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +> Input: nums = [3,2,4], target = 6 |
| 29 | +> |
| 30 | +> Output: [1,2] |
| 31 | +
|
| 32 | +**Example 3:** |
| 33 | + |
| 34 | +> Input: nums = [3,3], target = 6 |
| 35 | +> |
| 36 | +> Output: [0,1] |
| 37 | +
|
| 38 | +**Constraints:** |
| 39 | + |
| 40 | + * `2 <= nums.length <= 10^4` |
| 41 | + * `-10^9 <= nums[i] <= 10^9` |
| 42 | + * `-10^9 <= target <= 10^9` |
| 43 | + * **Only one valid answer exists.** |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +**Follow-up: **Can you come up with an algorithm that is less than `O(n2)` |
| 48 | +time complexity? |
| 49 | + |
| 50 | + |
| 51 | +## 题目大意 |
| 52 | + |
| 53 | +给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 **和为目标值** _`target`_ 的那 **两个** |
| 54 | +整数,并返回它们的数组下标。 |
| 55 | + |
| 56 | +你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。 |
| 57 | + |
| 58 | +你可以按任意顺序返回答案。 |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +**示例 1:** |
| 63 | + |
| 64 | +> |
| 65 | +> |
| 66 | +> |
| 67 | +> |
| 68 | +> |
| 69 | +> **输入:** nums = [2,7,11,15], target = 9 |
| 70 | +> |
| 71 | +> **输出:**[0,1] |
| 72 | +> |
| 73 | +> **解释:** 因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 |
| 74 | +> |
| 75 | +> |
| 76 | +
|
| 77 | +**示例 2:** |
| 78 | + |
| 79 | +> |
| 80 | +> |
| 81 | +> |
| 82 | +> |
| 83 | +> |
| 84 | +> **输入:** nums = [3,2,4], target = 6 |
| 85 | +> |
| 86 | +> **输出:**[1,2] |
| 87 | +> |
| 88 | +> |
| 89 | +
|
| 90 | +**示例 3:** |
| 91 | + |
| 92 | +> |
| 93 | +> |
| 94 | +> |
| 95 | +> |
| 96 | +> |
| 97 | +> **输入:** nums = [3,3], target = 6 |
| 98 | +> |
| 99 | +> **输出:**[0,1] |
| 100 | +> |
| 101 | +> |
| 102 | +
|
| 103 | + |
| 104 | + |
| 105 | +**提示:** |
| 106 | + |
| 107 | + * `2 <= nums.length <= 10^4` |
| 108 | + * `-10^9 <= nums[i] <= 10^9` |
| 109 | + * `-10^9 <= target <= 10^9` |
| 110 | + * **只会存在一个有效答案** |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +**进阶:** 你可以想出一个时间复杂度小于 `O(n2)` 的算法吗? |
| 115 | + |
| 116 | + |
| 117 | +## 解题思路 |
| 118 | + |
| 119 | +#### 复杂度分析 |
| 120 | + |
| 121 | +- **时间复杂度**:`O()`, |
| 122 | +- **空间复杂度**:`O()`, |
| 123 | + |
| 124 | +## 代码 |
| 125 | + |
| 126 | +```javascript |
| 127 | + |
| 128 | +``` |
| 129 | + |
| 130 | +## 相关题目 |
| 131 | + |
| 132 | +:::: md-demo 相关题目 |
| 133 | +- [三数之和](https://leetcode.com/problems/3sum) |
| 134 | +- [四数之和](https://leetcode.com/problems/4sum) |
| 135 | +- [两数之和 II - 输入有序数组](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) |
| 136 | +- [两数之和 III - 数据结构设计](https://leetcode.com/problems/two-sum-iii-data-structure-design) |
| 137 | +- [和为 K 的子数组](https://leetcode.com/problems/subarray-sum-equals-k) |
| 138 | +- [两数之和 IV - 输入二叉搜索树](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) |
| 139 | +- [小于 K 的两数之和](https://leetcode.com/problems/two-sum-less-than-k) |
| 140 | +- [K 和数对的最大数目](https://leetcode.com/problems/max-number-of-k-sum-pairs) |
| 141 | +- [大餐计数](https://leetcode.com/problems/count-good-meals) |
| 142 | +- [差的绝对值为 K 的数对数目](https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k) |
| 143 | +- [连接后等于目标字符串的字符串对](https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target) |
| 144 | +- [找出数组中的所有 K 近邻下标](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array) |
| 145 | +- [第一个出现两次的字母](https://leetcode.com/problems/first-letter-to-appear-twice) |
| 146 | +- [优质数对的数目](https://leetcode.com/problems/number-of-excellent-pairs) |
| 147 | +- [等差三元组的数目](https://leetcode.com/problems/number-of-arithmetic-triplets) |
| 148 | +- [边积分最高的节点](https://leetcode.com/problems/node-with-highest-edge-score) |
| 149 | +- [检查相同字母间的距离](https://leetcode.com/problems/check-distances-between-same-letters) |
| 150 | +- [和相等的子数组](https://leetcode.com/problems/find-subarrays-with-equal-sum) |
| 151 | +- [与对应负数同时存在的最大正整数](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) |
| 152 | +- [不同的平均值数目](https://leetcode.com/problems/number-of-distinct-averages) |
| 153 | +- [统计和小于目标的下标对数目](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target) |
| 154 | + |
| 155 | +:::: |
0 commit comments