|
| 1 | +--- |
| 2 | +title: leetcode1 两数之和【简单难度】 |
| 3 | +tags: |
| 4 | + - leetcode |
| 5 | + - 学习笔记 |
| 6 | + - 算法 |
| 7 | +--- |
| 8 | + |
| 9 | +### [1. 两数之和](https://leetcode-cn.com/problems/two-sum/) |
| 10 | + |
| 11 | +<table> <tr> <td bgcolor=white> ●くろまる 难度: </td> <td bgcolor=#5cb85c width=8.5%><font color=white>简单</font></td> <td bgcolor=white width=79%></td> </tr></table> |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 **和为目标值** *`target`* 的那 **两个** 整数,并返回它们的数组下标。 |
| 16 | + |
| 17 | +你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 |
| 18 | + |
| 19 | +你可以按任意顺序返回答案。 |
| 20 | + |
| 21 | +<br/> |
| 22 | + |
| 23 | +**示例 1:** |
| 24 | + |
| 25 | +``` |
| 26 | +输入:nums = [2,7,11,15], target = 9 |
| 27 | +输出:[0,1] |
| 28 | +解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 |
| 29 | +``` |
| 30 | + |
| 31 | +**示例 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +输入:nums = [3,2,4], target = 6 |
| 35 | +输出:[1,2] |
| 36 | +``` |
| 37 | + |
| 38 | +**示例 3:** |
| 39 | + |
| 40 | +``` |
| 41 | +输入:nums = [3,3], target = 6 |
| 42 | +输出:[0,1] |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +**提示:** |
| 48 | + |
| 49 | +- `2 <= nums.length <= 104` |
| 50 | +- `-109 <= nums[i] <= 109` |
| 51 | +- `-109 <= target <= 109` |
| 52 | +- **只会存在一个有效答案** |
| 53 | + |
| 54 | +**进阶:** 你可以想出一个时间复杂度小于 O(n<sup>2</sup>) 的算法吗? |
| 55 | + |
| 56 | +<br/> |
| 57 | + |
| 58 | +### 英文题目: 2 sum (Two sum) |
| 59 | + |
| 60 | +Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`. |
| 61 | +You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice. |
| 62 | +You can return the answer in any order. |
| 63 | + |
| 64 | +<br/> |
| 65 | + |
| 66 | +**Example 1:** |
| 67 | + |
| 68 | +**Input:** nums = [2,7,11,15], target = 9 |
| 69 | +**Output:** [0,1] |
| 70 | + |
| 71 | +**Explaination:** Because nums[0] + nums[1] == 9, we return [0, 1]. |
| 72 | + |
| 73 | + |
| 74 | +**Example 2:** |
| 75 | +**Input:** nums = [3,2,4], target = 6 |
| 76 | +**Output:** [1,2] |
| 77 | + |
| 78 | + |
| 79 | +**Example 3:** |
| 80 | +**Input:** nums = [3,3], target = 6 |
| 81 | +**Output:** [0,1] |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +**Constraints:** |
| 86 | + |
| 87 | +- `2 <= nums.length <= 103` |
| 88 | +- `-109 <= nums[i] <= 109` |
| 89 | +- `-109 <= target <= 109` |
| 90 | +- **Only one valid answer exists.** |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +### 分析: |
| 95 | + |
| 96 | +**方法1**: 暴力法,复杂度O(n^2),会TLE(超时); |
| 97 | + |
| 98 | +**方法2**: hashmap查表,在表中找 target - 当前循环变量i对应的那个数。用一个哈希表(C++中用unordered_map, C#中用dictionary, Python中用dict,Java中可以直接用HashMap),存储每个数对应的下标,复杂度O(n); |
| 99 | + |
| 100 | +**方法3**: 快排 + 双指针 |
| 101 | + |
| 102 | + |
| 103 | +### 方法2 AC代码: |
| 104 | + |
| 105 | +```cpp |
| 106 | +class Solution { |
| 107 | + public: |
| 108 | + vector<int> twoSum(vector<int> &nums, int target) |
| 109 | + { |
| 110 | + unordered_map<int, int> dict; |
| 111 | + vector<int> result; |
| 112 | + for(int i = 0; i < nums.size(); i++) { |
| 113 | + dict[nums[i]] = i; // 顺序的map映射: value->index |
| 114 | + } |
| 115 | + for(int i = 0; i < nums.size(); i++) |
| 116 | + { |
| 117 | + int query = target - nums[i]; |
| 118 | + if(dict.find(query) != dict.end() && dict[query] > i) // dict[query] > i是为了防止重复计算 |
| 119 | + { |
| 120 | + result.push_back(i); |
| 121 | + result.push_back(dict[query]); |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + return result; |
| 126 | + } |
| 127 | +}; |
| 128 | +``` |
| 129 | + |
| 130 | +### 方法2的另一种写法: |
| 131 | + |
| 132 | +```cpp |
| 133 | +class Solution { |
| 134 | +public: |
| 135 | + vector<int> twoSum(vector<int> &nums, int target) |
| 136 | + { |
| 137 | + unordered_map<int, int> dict; |
| 138 | + vector<int> res(2,-1), emptyVect; |
| 139 | + for(int i=0;i<nums.size();i++) |
| 140 | + { |
| 141 | + int query=target-nums[i]; |
| 142 | + if(dict.find(query)==dict.end()) dict[nums[i]]=i; // 逆序的map映射: value->index |
| 143 | + else { |
| 144 | + res[1]=i; |
| 145 | + res[0]=dict[query]; |
| 146 | + return res; |
| 147 | + } |
| 148 | + } |
| 149 | + return emptyVect; |
| 150 | + } |
| 151 | +}; |
| 152 | +``` |
| 153 | + |
| 154 | + |
| 155 | +### 方法3 AC代码: |
| 156 | + |
| 157 | +- 定义一个struct, 存储 index 和 value |
| 158 | +- 使用两个指针, l 和 r, l++, r-- |
| 159 | + |
| 160 | +left自增, right 自减 |
| 161 | + |
| 162 | + |
| 163 | +**注意**: 如果要在一个struct上调用STL中的sort方法,需要先为其定义好 compare 函数。 |
| 164 | + |
| 165 | + |
| 166 | +具体代码如下: |
| 167 | +```cpp |
| 168 | +typedef struct node{ |
| 169 | + int index; |
| 170 | + int value; |
| 171 | + node(){}; |
| 172 | + node(int i, int v) : index(i), value(v){} |
| 173 | +} Node; |
| 174 | + |
| 175 | +bool compare(const Node& a, const Node& b){ |
| 176 | + return a.value < b.value; |
| 177 | +} |
| 178 | + |
| 179 | +class Solution { |
| 180 | +public: |
| 181 | + vector<int> twoSum(vector<int> &nums, int target) { |
| 182 | + |
| 183 | + int len = nums.size(); |
| 184 | + assert(len >= 2); |
| 185 | + |
| 186 | + vector<int> res(2, 0); // 初始化:res包含2个值为0的元素 |
| 187 | + |
| 188 | + vector<Node> nums2(len); |
| 189 | + for(int i = 0; i < len; i++){ |
| 190 | + nums2[i] = Node(i+1, nums[i]); |
| 191 | + } |
| 192 | + |
| 193 | + sort(nums2.begin(), nums2.end(), compare); // 在定义的struct上调用快排,T(n)=O(n*log(n)) |
| 194 | + |
| 195 | + int l = 0; |
| 196 | + int r = len - 1; |
| 197 | + while(l < r){ |
| 198 | + int sum = nums2[l].value + nums2[r].value; |
| 199 | + if(sum == target){ |
| 200 | + res[0] = min(nums2[l].index, nums2[r].index)-1; // 注意,这里需要减去1 |
| 201 | + res[1] = max(nums2[l].index, nums2[r].index)-1; |
| 202 | + break; |
| 203 | + } else if(sum < target){ |
| 204 | + l++; |
| 205 | + } else { |
| 206 | + r--; |
| 207 | + } |
| 208 | + } |
| 209 | + return res; // 用两个指针来扫 |
| 210 | + } |
| 211 | +}; |
| 212 | +``` |
| 213 | + |
0 commit comments