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

@@ -9,10 +9,10 @@ package chapter_searching;
import java.util.*;
public class two_sum {
/* Method 1: Brute force enumeration */
/* Method 1: brute-force enumeration */
static int[] twoSumBruteForce(int[] nums, int target) {
int size = nums.length;
// Two nested loops, time complexity is O(n^2)
// Two nested loops give a time complexity of O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
@@ -22,12 +22,12 @@ public class two_sum {
return new int[0];
}
/* Method 2: Auxiliary hash table */
/* Method 2: auxiliary hash table */
static int[] twoSumHashTable(int[] nums, int target) {
int size = nums.length;
// Auxiliary hash table, space complexity is O(n)
// The auxiliary hash table uses O(n) space
Map<Integer, Integer> dic = new HashMap<>();
// Single loop, time complexity is O(n)
// A single loop gives a time complexity of O(n)
for (int i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return new int[] { dic.get(target - nums[i]), i };
@@ -38,16 +38,16 @@ public class two_sum {
}
public static void main(String[] args) {
// ======= Test Case =======
// ======= Test case =======
int[] nums = { 2, 7, 11, 15 };
int target = 13;
// ====== Driver Code ======
// ====== Driver code ======
// Method 1
int[] res = twoSumBruteForce(nums, target);
System.out.println("Method 1 res = " + Arrays.toString(res));
System.out.println("Method 1: res = " + Arrays.toString(res));
// Method 2
res = twoSumHashTable(nums, target);
System.out.println("Method 2 res = " + Arrays.toString(res));
System.out.println("Method 2: res = " + Arrays.toString(res));
}
}