mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 05:30:59 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* File: binary_search.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class binary_search {
|
||||
/* Binary search (closed interval on both sides) */
|
||||
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
|
||||
int i = 0, j = nums.Length - 1;
|
||||
// Loop, exit when the search interval is empty (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]
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This means target is in the interval [i, m-1]
|
||||
j = m - 1;
|
||||
else // Found the target element, return its index
|
||||
return m;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
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
|
||||
int i = 0, j = nums.Length;
|
||||
// Loop, exit when the search interval is empty (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)
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This means target is in the interval [i, m)
|
||||
j = m;
|
||||
else // Found the target element, return its index
|
||||
return m;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 6;
|
||||
int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
int index = BinarySearch(nums, target);
|
||||
Console.WriteLine("Index of target element 6 = " + index);
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
index = BinarySearchLCRO(nums, target);
|
||||
Console.WriteLine("Index of target element 6 = " + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: binary_search_edge.cs
|
||||
* Created Time: 2023-08-06
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class binary_search_edge {
|
||||
/* Binary search for the leftmost target */
|
||||
int BinarySearchLeftEdge(int[] nums, int target) {
|
||||
// Equivalent to finding the insertion point of target
|
||||
int i = binary_search_insertion.BinarySearchInsertion(nums, target);
|
||||
// Target not found, return -1
|
||||
if (i == nums.Length || nums[i] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for the rightmost target */
|
||||
int BinarySearchRightEdge(int[] nums, int target) {
|
||||
// Convert to finding the leftmost 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
|
||||
int j = i - 1;
|
||||
// Target not found, return -1
|
||||
if (j == -1 || nums[j] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index j
|
||||
return j;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Array with duplicate elements
|
||||
int[] nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
Console.WriteLine("\nArray nums = " + nums.PrintList());
|
||||
|
||||
// Binary search left and right boundaries
|
||||
foreach (int target in new int[] { 6, 7 }) {
|
||||
int index = BinarySearchLeftEdge(nums, target);
|
||||
Console.WriteLine("Leftmost element " + target + " has index " + index);
|
||||
index = BinarySearchRightEdge(nums, target);
|
||||
Console.WriteLine("Rightmost element " + target + " has index " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* File: binary_search_insertion.cs
|
||||
* Created Time: 2023-08-06
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class binary_search_insertion {
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
public static int BinarySearchInsertionSimple(int[] nums, int target) {
|
||||
int i = 0, j = nums.Length - 1; // Initialize 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]
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
} else {
|
||||
return m; // Found target, return insertion point m
|
||||
}
|
||||
}
|
||||
// Target not found, return insertion point i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
public static int BinarySearchInsertion(int[] nums, int target) {
|
||||
int i = 0, j = nums.Length - 1; // Initialize 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]
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
} else {
|
||||
j = m - 1; // The first element less than target is in the interval [i, m-1]
|
||||
}
|
||||
}
|
||||
// Return insertion point i
|
||||
return i;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Array without duplicate elements
|
||||
int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
Console.WriteLine("\nArray nums = " + nums.PrintList());
|
||||
// Binary search for insertion point
|
||||
foreach (int target in new int[] { 6, 9 }) {
|
||||
int index = BinarySearchInsertionSimple(nums, target);
|
||||
Console.WriteLine("Element " + target + "'s insertion point index is " + index);
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
Console.WriteLine("\nArray nums = " + nums.PrintList());
|
||||
// Binary search for insertion point
|
||||
foreach (int target in new int[] { 2, 6, 20 }) {
|
||||
int index = BinarySearchInsertion(nums, target);
|
||||
Console.WriteLine("Element " + target + "'s insertion point index is " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: hashing_search.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class hashing_search {
|
||||
/* Hash search (array) */
|
||||
int HashingSearchArray(Dictionary<int, int> map, int target) {
|
||||
// Hash table's key: target element, value: index
|
||||
// If this key does not exist in the hash table, return -1
|
||||
return map.GetValueOrDefault(target, -1);
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
|
||||
|
||||
// Hash table key: target node value, value: node object
|
||||
// If key is not in hash table, return null
|
||||
return map.GetValueOrDefault(target);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 3;
|
||||
|
||||
/* Hash search (array) */
|
||||
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
// Initialize hash table
|
||||
Dictionary<int, int> map = [];
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
map[nums[i]] = i; // key: element, value: index
|
||||
}
|
||||
int index = HashingSearchArray(map, target);
|
||||
Console.WriteLine("Index of target element 3 = " + index);
|
||||
|
||||
/* Hash search (linked list) */
|
||||
ListNode? head = ListNode.ArrToLinkedList(nums);
|
||||
// Initialize hash table
|
||||
Dictionary<int, ListNode> map1 = [];
|
||||
while (head != null) {
|
||||
map1[head.val] = head; // key: node value, value: node
|
||||
head = head.next;
|
||||
}
|
||||
ListNode? node = HashingSearchLinkedList(map1, target);
|
||||
Console.WriteLine("Node object corresponding to target node value 3 is " + node);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: linear_search.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class linear_search {
|
||||
/* Linear search (array) */
|
||||
int LinearSearchArray(int[] nums, int target) {
|
||||
// Traverse array
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
// Found the target element, return its index
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Linear search (linked list) */
|
||||
ListNode? LinearSearchLinkedList(ListNode? head, int target) {
|
||||
// Traverse the linked list
|
||||
while (head != null) {
|
||||
// Found the target node, return it
|
||||
if (head.val == target)
|
||||
return head;
|
||||
head = head.next;
|
||||
}
|
||||
// Target node not found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 3;
|
||||
|
||||
/* Perform linear search in array */
|
||||
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
int index = LinearSearchArray(nums, target);
|
||||
Console.WriteLine("Index of target element 3 = " + index);
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
ListNode? head = ListNode.ArrToLinkedList(nums);
|
||||
ListNode? node = LinearSearchLinkedList(head, target);
|
||||
Console.WriteLine("Node object corresponding to target node value 3 is " + node);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: two_sum.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class two_sum {
|
||||
/* Method 1: Brute force enumeration */
|
||||
int[] TwoSumBruteForce(int[] nums, int target) {
|
||||
int size = nums.Length;
|
||||
// Two nested loops, time complexity is 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)
|
||||
return [i, j];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/* Method 2: Auxiliary hash table */
|
||||
int[] TwoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.Length;
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
Dictionary<int, int> dic = [];
|
||||
// Single loop, time complexity is O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.ContainsKey(target - nums[i])) {
|
||||
return [dic[target - nums[i]], i];
|
||||
}
|
||||
dic.Add(nums[i], i);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// ======= Test Case =======
|
||||
int[] nums = [2, 7, 11, 15];
|
||||
int target = 13;
|
||||
|
||||
// ====== Driver Code ======
|
||||
// Method 1
|
||||
int[] res = TwoSumBruteForce(nums, target);
|
||||
Console.WriteLine("Method 1 res = " + string.Join(",", res));
|
||||
// Method 2
|
||||
res = TwoSumHashTable(nums, target);
|
||||
Console.WriteLine("Method 2 res = " + string.Join(",", res));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user