mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 05:33:40 +08:00
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:
@@ -7,39 +7,39 @@
|
||||
package chapter_searching;
|
||||
|
||||
public class binary_search {
|
||||
/* Binary search (closed interval on both sides) */
|
||||
/* Binary search (closed interval) */
|
||||
static int binarySearch(int[] nums, int target) {
|
||||
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
|
||||
// Initialize the closed interval [0, n-1], with i and j pointing to the first and last elements
|
||||
int i = 0, j = nums.length - 1;
|
||||
// Loop, exit when the search interval is empty (empty when i > j)
|
||||
// Loop until the search interval is empty (when i > j)
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) // This means target is in the interval [m+1, j]
|
||||
if (nums[m] < target) // In this case, target must be in [m+1, j] if it exists
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This means target is in the interval [i, m-1]
|
||||
else if (nums[m] > target) // In this case, target must be in [i, m-1] if it exists
|
||||
j = m - 1;
|
||||
else // Found the target element, return its index
|
||||
else // The target element was found; return its index
|
||||
return m;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
// The target element was not found; return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
/* Binary search (left-closed, right-open interval) */
|
||||
static int binarySearchLCRO(int[] nums, int target) {
|
||||
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
|
||||
// Initialize the left-closed, right-open interval [0, n), with i at the first element and j one past the last
|
||||
int i = 0, j = nums.length;
|
||||
// Loop, exit when the search interval is empty (empty when i = j)
|
||||
// Loop until the search interval is empty (when i = j)
|
||||
while (i < j) {
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) // This means target is in the interval [m+1, j)
|
||||
if (nums[m] < target) // In this case, target must be in [m+1, j) if it exists
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This means target is in the interval [i, m)
|
||||
else if (nums[m] > target) // In this case, target must be in [i, m) if it exists
|
||||
j = m;
|
||||
else // Found the target element, return its index
|
||||
else // The target element was found; return its index
|
||||
return m;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
// The target element was not found; return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -47,11 +47,11 @@ public class binary_search {
|
||||
int target = 6;
|
||||
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
/* Binary search (closed interval) */
|
||||
int index = binarySearch(nums, target);
|
||||
System.out.println("Index of target element 6 = " + index);
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
/* Binary search (left-closed, right-open interval) */
|
||||
index = binarySearchLCRO(nums, target);
|
||||
System.out.println("Index of target element 6 = " + index);
|
||||
}
|
||||
|
||||
@@ -7,43 +7,43 @@
|
||||
package chapter_searching;
|
||||
|
||||
public class binary_search_edge {
|
||||
/* Binary search for the leftmost target */
|
||||
/* Find the leftmost occurrence of target using binary search */
|
||||
static int binarySearchLeftEdge(int[] nums, int target) {
|
||||
// Equivalent to finding the insertion point of target
|
||||
// This is equivalent to finding the insertion point of target
|
||||
int i = binary_search_insertion.binarySearchInsertion(nums, target);
|
||||
// Target not found, return -1
|
||||
// If target was not found, return -1
|
||||
if (i == nums.length || nums[i] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index i
|
||||
// Target was found; return index i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for the rightmost target */
|
||||
/* Find the rightmost occurrence of target using binary search */
|
||||
static int binarySearchRightEdge(int[] nums, int target) {
|
||||
// Convert to finding the leftmost target + 1
|
||||
// Reduce the problem to finding the leftmost insertion point of target + 1
|
||||
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
// j points to the rightmost target, while i points to the first element greater than target
|
||||
int j = i - 1;
|
||||
// Target not found, return -1
|
||||
// If target was not found, return -1
|
||||
if (j == -1 || nums[j] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index j
|
||||
// Target was found; return index j
|
||||
return j;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Array with duplicate elements
|
||||
// Array containing duplicate elements
|
||||
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
|
||||
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
|
||||
|
||||
// Binary search left and right boundaries
|
||||
// Find the left and right boundaries using binary search
|
||||
for (int target : new int[] { 6, 7 }) {
|
||||
int index = binarySearchLeftEdge(nums, target);
|
||||
System.out.println("Index of leftmost element " + target + " is " + index);
|
||||
System.out.println("Leftmost occurrence of " + target + " has index " + index);
|
||||
index = binarySearchRightEdge(nums, target);
|
||||
System.out.println("Index of rightmost element " + target + " is " + index);
|
||||
System.out.println("Rightmost occurrence of " + target + " has index " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,34 +7,34 @@
|
||||
package chapter_searching;
|
||||
|
||||
class binary_search_insertion {
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
/* Find an insertion point using binary search (no duplicates) */
|
||||
static int binarySearchInsertionSimple(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]
|
||||
int i = 0, j = nums.length - 1; // Initialize the closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
i = m + 1; // Target must be in [m+1, j] if it exists
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
j = m - 1; // Target must be in [i, m-1] if it exists
|
||||
} else {
|
||||
return m; // Found target, return insertion point m
|
||||
return m; // Target was found; return insertion point m
|
||||
}
|
||||
}
|
||||
// Target not found, return insertion point i
|
||||
// Target was not found; return insertion point i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
/* Find an insertion point using binary search (with duplicates) */
|
||||
static int binarySearchInsertion(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]
|
||||
int i = 0, j = nums.length - 1; // Initialize the closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
i = m + 1; // Target must be in [m+1, j] if it exists
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
j = m - 1; // Target must be in [i, m-1] if it exists
|
||||
} else {
|
||||
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
|
||||
j = m - 1; // Continue in [i, m-1] to find the rightmost element smaller than target
|
||||
}
|
||||
}
|
||||
// Return insertion point i
|
||||
@@ -42,22 +42,22 @@ class binary_search_insertion {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Array without duplicate elements
|
||||
// Array with no duplicate elements
|
||||
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
|
||||
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
|
||||
// Binary search for insertion point
|
||||
// Find an insertion point using binary search
|
||||
for (int target : new int[] { 6, 9 }) {
|
||||
int index = binarySearchInsertionSimple(nums, target);
|
||||
System.out.println("Insertion point index for element " + target + " is " + index);
|
||||
System.out.println("Element " + target + " has insertion point index " + index);
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
// Array containing duplicate elements
|
||||
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
|
||||
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
|
||||
// Binary search for insertion point
|
||||
// Find an insertion point using binary search
|
||||
for (int target : new int[] { 2, 6, 20 }) {
|
||||
int index = binarySearchInsertion(nums, target);
|
||||
System.out.println("Insertion point index for element " + target + " is " + index);
|
||||
System.out.println("Element " + target + " has insertion point index " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,42 +10,42 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class hashing_search {
|
||||
/* Hash search (array) */
|
||||
/* Hash-based search (array) */
|
||||
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
|
||||
// Hash table's key: target element, value: index
|
||||
// If this key does not exist in the hash table, return -1
|
||||
// Hash table key: target element; value: index
|
||||
// Return -1 if the hash table does not contain this key
|
||||
return map.getOrDefault(target, -1);
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
/* Hash-based search (linked list) */
|
||||
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
|
||||
// Hash table key: target node value, value: node object
|
||||
// If key is not in hash table, return null
|
||||
// Hash table key: target node value; value: node object
|
||||
// Return null if the hash table does not contain this key
|
||||
return map.getOrDefault(target, null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int target = 3;
|
||||
|
||||
/* Hash search (array) */
|
||||
/* Hash-based search (array) */
|
||||
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
|
||||
// Initialize hash table
|
||||
// Initialize the hash table
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
map.put(nums[i], i); // key: element, value: index
|
||||
map.put(nums[i], i); // key: element; value: index
|
||||
}
|
||||
int index = hashingSearchArray(map, target);
|
||||
System.out.println("Index of target element 3 = " + index);
|
||||
|
||||
/* Hash search (linked list) */
|
||||
/* Hash-based search (linked list) */
|
||||
ListNode head = ListNode.arrToLinkedList(nums);
|
||||
// Initialize hash table
|
||||
// Initialize the hash table
|
||||
Map<Integer, ListNode> map1 = new HashMap<>();
|
||||
while (head != null) {
|
||||
map1.put(head.val, head); // key: node value, value: node
|
||||
map1.put(head.val, head); // key: node value; value: node
|
||||
head = head.next;
|
||||
}
|
||||
ListNode node = hashingSearchLinkedList(map1, target);
|
||||
System.out.println("Node object corresponding to target node value 3 is " + node);
|
||||
System.out.println("The node object corresponding to target value 3 is " + node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ import utils.*;
|
||||
public class linear_search {
|
||||
/* Linear search (array) */
|
||||
static int linearSearchArray(int[] nums, int target) {
|
||||
// Traverse array
|
||||
// Traverse the array
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// Found the target element, return its index
|
||||
// The target element was found; return its index
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
// The target element was not found; return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -25,26 +25,26 @@ public class linear_search {
|
||||
static ListNode linearSearchLinkedList(ListNode head, int target) {
|
||||
// Traverse the linked list
|
||||
while (head != null) {
|
||||
// Found the target node, return it
|
||||
// The target node was found; return it
|
||||
if (head.val == target)
|
||||
return head;
|
||||
head = head.next;
|
||||
}
|
||||
// Target node not found, return null
|
||||
// The target node was not found; return null
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int target = 3;
|
||||
|
||||
/* Perform linear search in array */
|
||||
/* Perform linear search on the array */
|
||||
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
|
||||
int index = linearSearchArray(nums, target);
|
||||
System.out.println("Index of target element 3 = " + index);
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
/* Perform linear search on the linked list */
|
||||
ListNode head = ListNode.arrToLinkedList(nums);
|
||||
ListNode node = linearSearchLinkedList(head, target);
|
||||
System.out.println("Node object corresponding to target node value 3 is " + node);
|
||||
System.out.println("The node object corresponding to target value 3 is " + node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user