Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,8 +6,8 @@ Author: krahets (krahets@163.com)
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
"""Method 1: Brute force enumeration"""
# Two nested loops, time complexity is O(n^2)
"""Method 1: brute-force enumeration"""
# Two nested loops give a time complexity of O(n^2)
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
@@ -16,10 +16,10 @@ def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
"""Method 2: Auxiliary hash table"""
# Auxiliary hash table, space complexity is O(n)
"""Method 2: auxiliary hash table"""
# The auxiliary hash table uses O(n) space
dic = {}
# Single loop, time complexity is O(n)
# A single loop with average O(1) hash operations gives an average time complexity of O(n)
for i in range(len(nums)):
if target - nums[i] in dic:
return [dic[target - nums[i]], i]
@@ -27,16 +27,16 @@ def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
return []
"""Driver Code"""
"""Driver code"""
if __name__ == "__main__":
# ======= Test Case =======
# ======= Test case =======
nums = [2, 7, 11, 15]
target = 13
# ====== Driver Code ======
# ====== Driver code ======
# Method 1
res: list[int] = two_sum_brute_force(nums, target)
print("Method 1 res =", res)
print("Method 1: res =", res)
# Method 2
res: list[int] = two_sum_hash_table(nums, target)
print("Method 2 res =", res)
print("Method 2: res =", res)