LeetCode 001 两数之和
有勇气的牛排
29
算法
2025-08-03 22:57:35
题目
知识点:数组、哈希表
给定一个整数数组 nums
和一个整数目标值 target
,请你再该数组中找出 和为目标值 target
的那两个整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案,并且你不能使用两次想同的元素。
你可以按任意顺序返回答案。
示例1:
输入:nums = [2, 7, 11, 15], target=9
输出:[0, 1]
解释:因为 nums[0] + nums[1] == 9, 返回[0, 1]
示例2:
输入:nums = [3, 2, 4],target = 6
输出[1, 2]
示例3:
输入:nums = [3, 3],target = 6
输出:[0, 1]
提示:
2 <= nums.length <= 104
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9
- 只会存在一个有效答案
暴力枚举(双层循环)
思路:枚举数组中所有可能的两两组合,检查它们的和是否等于target
。
时间复杂度:O(n²)
空间复杂度:O(1)
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
print(i, j)
if nums[i] + nums[j] == target:
return [i, j]
return []
print(Solution().twoSum([2, 7, 11, 15], 9))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([3, 3], 6))
组合
0 1
0 2
0 3
1 2
1 3
2 3
哈希表(一次遍历)
思路:
- 使用字典(哈希表)存储已经遍历过的数字及其下标。
- 每次遍历一个数 num,计算它需要的补数,
complement = target - num
- 如果补数在哈希表中,说明找到了答案
时间复杂度:O(n)
空间复杂度:O(n)
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
lookup = {}
for index, num in enumerate(nums):
complement = target - num
print(index, num, complement)
if complement in lookup:
return [lookup[complement], index]
else:
lookup[num] = index
return []
print(Solution().twoSum([2, 7, 11, 15], 9))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([3, 3], 6))
一句话描述:
哈希记录 已遍历元素+下标,后面元素如果补数匹配到前面结果,即成功。
<h2><a id="_0"></a>题目</h2>
<p>知识点:数组、哈希表</p>
<p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>,请你再该数组中找出 <strong>和为目标值</strong> <code>target</code> 的那两个整数,并返回它们的数组下标。</p>
<p>你可以假设每种输入只会对应一个答案,并且你不能使用两次想同的元素。</p>
<p>你可以按任意顺序返回答案。</p>
<p>示例1:</p>
<blockquote>
<p>输入:nums = [2, 7, 11, 15], target=9</p>
<p>输出:[0, 1]</p>
<p>解释:因为 nums[0] + nums[1] == 9, 返回[0, 1]</p>
</blockquote>
<p>示例2:</p>
<blockquote>
<p>输入:nums = [3, 2, 4],target = 6</p>
<p>输出[1, 2]</p>
</blockquote>
<p>示例3:</p>
<blockquote>
<p>输入:nums = [3, 3],target = 6</p>
<p>输出:[0, 1]</p>
</blockquote>
<p>提示:</p>
<ul>
<li><code>2 <= nums.length <= 104</code></li>
<li><code>-10^9 <= nums[i] <= 10^9</code></li>
<li><code>-10^9 <= target <= 10^9</code></li>
<li>只会存在一个有效答案</li>
</ul>
<h2><a id="_37"></a>暴力枚举(双层循环)</h2>
<p>思路:枚举数组中所有可能的两两组合,检查它们的和是否等于<code>target</code>。</p>
<p>时间复杂度:O(n²)</p>
<p>空间复杂度:O(1)</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>:
<span class="hljs-keyword">def</span> <span class="hljs-title function_">twoSum</span>(<span class="hljs-params">self, nums: <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>], target: <span class="hljs-built_in">int</span></span>) -> <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>]:
n = <span class="hljs-built_in">len</span>(nums)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n):
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(i + <span class="hljs-number">1</span>, n):
<span class="hljs-built_in">print</span>(i, j)
<span class="hljs-keyword">if</span> nums[i] + nums[j] == target:
<span class="hljs-keyword">return</span> [i, j]
<span class="hljs-keyword">return</span> []
<span class="hljs-built_in">print</span>(Solution().twoSum([<span class="hljs-number">2</span>, <span class="hljs-number">7</span>, <span class="hljs-number">11</span>, <span class="hljs-number">15</span>], <span class="hljs-number">9</span>)) <span class="hljs-comment"># [0, 1]</span>
<span class="hljs-built_in">print</span>(Solution().twoSum([<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>], <span class="hljs-number">6</span>)) <span class="hljs-comment"># [1, 2]</span>
<span class="hljs-built_in">print</span>(Solution().twoSum([<span class="hljs-number">3</span>, <span class="hljs-number">3</span>], <span class="hljs-number">6</span>)) <span class="hljs-comment"># [0, 1]</span>
</code></div></pre>
<p>组合</p>
<pre><div class="hljs"><code class="lang-shell">0 1
0 2
0 3
1 2
1 3
2 3
</code></div></pre>
<h2><a id="_76"></a>哈希表(一次遍历)</h2>
<p>思路:</p>
<ol>
<li>使用字典(哈希表)存储已经遍历过的数字及其下标。</li>
<li>每次遍历一个数 num,计算它需要的补数,<code>complement = target - num</code></li>
<li>如果补数在哈希表中,说明找到了答案</li>
</ol>
<p>时间复杂度:O(n)</p>
<p>空间复杂度:O(n)</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>:
<span class="hljs-keyword">def</span> <span class="hljs-title function_">twoSum</span>(<span class="hljs-params">self, nums: <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>], target: <span class="hljs-built_in">int</span></span>) -> <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>]:
lookup = {}
<span class="hljs-keyword">for</span> index, num <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(nums):
complement = target - num
<span class="hljs-built_in">print</span>(index, num, complement)
<span class="hljs-keyword">if</span> complement <span class="hljs-keyword">in</span> lookup:
<span class="hljs-comment"># 补数在已经遍历过的记录中存在</span>
<span class="hljs-keyword">return</span> [lookup[complement], index]
<span class="hljs-keyword">else</span>:
<span class="hljs-comment"># 不存在则添加记录,并记录索引</span>
lookup[num] = index
<span class="hljs-keyword">return</span> []
<span class="hljs-built_in">print</span>(Solution().twoSum([<span class="hljs-number">2</span>, <span class="hljs-number">7</span>, <span class="hljs-number">11</span>, <span class="hljs-number">15</span>], <span class="hljs-number">9</span>)) <span class="hljs-comment"># [0, 1]</span>
<span class="hljs-built_in">print</span>(Solution().twoSum([<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>], <span class="hljs-number">6</span>)) <span class="hljs-comment"># [1, 2]</span>
<span class="hljs-built_in">print</span>(Solution().twoSum([<span class="hljs-number">3</span>, <span class="hljs-number">3</span>], <span class="hljs-number">6</span>)) <span class="hljs-comment"># [0, 1]</span>
</code></div></pre>
<p>一句话描述:</p>
<p>哈希记录 已遍历元素+下标,后面元素如果补数匹配到前面结果,即成功。</p>
评论区