mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-20 05:17:15 +08: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,43 @@
|
||||
// File: binary_search.go
|
||||
// Created Time: 2022-12-05
|
||||
// Author: Slone123c (274325721@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
func binarySearch(nums []int, target int) int {
|
||||
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
|
||||
i, j := 0, len(nums)-1
|
||||
// Loop, exit when the search interval is empty (empty when i > j)
|
||||
for i <= j {
|
||||
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) */
|
||||
func binarySearchLCRO(nums []int, target int) int {
|
||||
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
|
||||
i, j := 0, len(nums)
|
||||
// Loop, exit when the search interval is empty (empty when i = j)
|
||||
for i < j {
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// File: binary_search_edge.go
|
||||
// Created Time: 2023-08-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
/* Binary search for the leftmost target */
|
||||
func binarySearchLeftEdge(nums []int, target int) int {
|
||||
// Equivalent to finding the insertion point of target
|
||||
i := binarySearchInsertion(nums, target)
|
||||
// Target not found, return -1
|
||||
if i == len(nums) || nums[i] != target {
|
||||
return -1
|
||||
}
|
||||
// Found target, return index i
|
||||
return i
|
||||
}
|
||||
|
||||
/* Binary search for the rightmost target */
|
||||
func binarySearchRightEdge(nums []int, target int) int {
|
||||
// Convert to finding the leftmost target + 1
|
||||
i := binarySearchInsertion(nums, target+1)
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
j := i - 1
|
||||
// Target not found, return -1
|
||||
if j == -1 || nums[j] != target {
|
||||
return -1
|
||||
}
|
||||
// Found target, return index j
|
||||
return j
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// File: binary_search_insertion.go
|
||||
// Created Time: 2023-08-23
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
func binarySearchInsertionSimple(nums []int, target int) int {
|
||||
// Initialize closed interval [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
for i <= j {
|
||||
// Calculate the midpoint index m
|
||||
m := i + (j-i)/2
|
||||
if nums[m] < target {
|
||||
// target is in the interval [m+1, j]
|
||||
i = m + 1
|
||||
} else if nums[m] > target {
|
||||
// target is in the interval [i, m-1]
|
||||
j = m - 1
|
||||
} else {
|
||||
// Found target, return insertion point m
|
||||
return m
|
||||
}
|
||||
}
|
||||
// Target not found, return insertion point i
|
||||
return i
|
||||
}
|
||||
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
func binarySearchInsertion(nums []int, target int) int {
|
||||
// Initialize closed interval [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
for i <= j {
|
||||
// Calculate the midpoint index m
|
||||
m := i + (j-i)/2
|
||||
if nums[m] < target {
|
||||
// target is in the interval [m+1, j]
|
||||
i = m + 1
|
||||
} else if nums[m] > target {
|
||||
// target is in the interval [i, m-1]
|
||||
j = m - 1
|
||||
} else {
|
||||
// The first element less than target is in the interval [i, m-1]
|
||||
j = m - 1
|
||||
}
|
||||
}
|
||||
// Return insertion point i
|
||||
return i
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// File: binary_search_test.go
|
||||
// Created Time: 2022-12-05
|
||||
// Author: Slone123c (274325721@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBinarySearch(t *testing.T) {
|
||||
var (
|
||||
target = 6
|
||||
nums = []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
expected = 2
|
||||
)
|
||||
// Perform binary search in array
|
||||
actual := binarySearch(nums, target)
|
||||
fmt.Println("Index of target element 6 =", actual)
|
||||
if actual != expected {
|
||||
t.Errorf("Index of target element 6 = %d, should be %d", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinarySearchEdge(t *testing.T) {
|
||||
// Array with duplicate elements
|
||||
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
fmt.Println("\nArray nums = ", nums)
|
||||
|
||||
// Binary search left and right boundaries
|
||||
for _, target := range []int{6, 7} {
|
||||
index := binarySearchLeftEdge(nums, target)
|
||||
fmt.Println("Leftmost element", target, " index is", index)
|
||||
|
||||
index = binarySearchRightEdge(nums, target)
|
||||
fmt.Println("Rightmost element", target, " index is", index)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinarySearchInsertion(t *testing.T) {
|
||||
// Array without duplicate elements
|
||||
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
fmt.Println("Array nums =", nums)
|
||||
|
||||
// Binary search for insertion point
|
||||
for _, target := range []int{6, 9} {
|
||||
index := binarySearchInsertionSimple(nums, target)
|
||||
fmt.Println("Element", target, " insertion point index is", index)
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
nums = []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
|
||||
fmt.Println("\nArray nums =", nums)
|
||||
|
||||
// Binary search for insertion point
|
||||
for _, target := range []int{2, 6, 20} {
|
||||
index := binarySearchInsertion(nums, target)
|
||||
fmt.Println("Element", target, " insertion point index is", index)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// File: hashing_search.go
|
||||
// Created Time: 2022-12-12
|
||||
// Author: Slone123c (274325721@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import . "github.com/krahets/hello-algo/pkg"
|
||||
|
||||
/* Hash search (array) */
|
||||
func hashingSearchArray(m map[int]int, target int) int {
|
||||
// Hash table's key: target element, value: index
|
||||
// If this key does not exist in the hash table, return -1
|
||||
if index, ok := m[target]; ok {
|
||||
return index
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
func hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode {
|
||||
// Hash table key: target node value, value: node object
|
||||
// Return nil if key does not exist in hash table
|
||||
if node, ok := m[target]; ok {
|
||||
return node
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// File: hashing_search_test.go
|
||||
// Created Time: 2022-12-12
|
||||
// Author: Slone123c (274325721@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestHashingSearch(t *testing.T) {
|
||||
target := 3
|
||||
/* Hash search (array) */
|
||||
nums := []int{1, 5, 3, 2, 4, 7, 5, 9, 10, 8}
|
||||
// Initialize hash table
|
||||
m := make(map[int]int)
|
||||
for i := 0; i < len(nums); i++ {
|
||||
m[nums[i]] = i
|
||||
}
|
||||
index := hashingSearchArray(m, target)
|
||||
fmt.Println("Index of target element 3 = ", index)
|
||||
|
||||
/* Hash search (linked list) */
|
||||
head := ArrayToLinkedList(nums)
|
||||
// Initialize hash table
|
||||
m1 := make(map[int]*ListNode)
|
||||
for head != nil {
|
||||
m1[head.Val] = head
|
||||
head = head.Next
|
||||
}
|
||||
node := hashingSearchLinkedList(m1, target)
|
||||
fmt.Println("Node object corresponding to target node value 3 is ", node)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// File: linear_search.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
/* Linear search (array) */
|
||||
func linearSearchArray(nums []int, target int) int {
|
||||
// Traverse array
|
||||
for i := 0; i < len(nums); 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) */
|
||||
func linearSearchLinkedList(node *ListNode, target int) *ListNode {
|
||||
// Traverse the linked list
|
||||
for node != nil {
|
||||
// Found the target node, return it
|
||||
if node.Val == target {
|
||||
return node
|
||||
}
|
||||
node = node.Next
|
||||
}
|
||||
// Target element not found, return nil
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// File: linear_search_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestLinearSearch(t *testing.T) {
|
||||
target := 3
|
||||
nums := []int{1, 5, 3, 2, 4, 7, 5, 9, 10, 8}
|
||||
|
||||
// Perform linear search in array
|
||||
index := linearSearchArray(nums, target)
|
||||
fmt.Println("Index of target element 3 =", index)
|
||||
|
||||
// Perform linear search in linked list
|
||||
head := ArrayToLinkedList(nums)
|
||||
node := linearSearchLinkedList(head, target)
|
||||
fmt.Println("Node object with target value 3 is", node)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// File: two_sum.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
/* Method 1: Brute force enumeration */
|
||||
func twoSumBruteForce(nums []int, target int) []int {
|
||||
size := len(nums)
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for i := 0; i < size-1; i++ {
|
||||
for j := i + 1; j < size; j++ {
|
||||
if nums[i]+nums[j] == target {
|
||||
return []int{i, j}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* Method 2: Auxiliary hash table */
|
||||
func twoSumHashTable(nums []int, target int) []int {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
hashTable := map[int]int{}
|
||||
// Single loop, time complexity is O(n)
|
||||
for idx, val := range nums {
|
||||
if preIdx, ok := hashTable[target-val]; ok {
|
||||
return []int{preIdx, idx}
|
||||
}
|
||||
hashTable[val] = idx
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// File: two_sum_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTwoSum(t *testing.T) {
|
||||
// ======= Test Case =======
|
||||
nums := []int{2, 7, 11, 15}
|
||||
target := 13
|
||||
|
||||
// ====== Driver Code ======
|
||||
// Method 1: Brute-force approach
|
||||
res := twoSumBruteForce(nums, target)
|
||||
fmt.Println("Method 1 res =", res)
|
||||
// Method 2: Hash table
|
||||
res = twoSumHashTable(nums, target)
|
||||
fmt.Println("Method 2 res =", res)
|
||||
}
|
||||
Reference in New Issue
Block a user