mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-22 14:26:34 +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,62 @@
|
||||
/**
|
||||
* File: binary_search.swift
|
||||
* Created Time: 2023-01-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 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
|
||||
var i = nums.startIndex
|
||||
var j = nums.endIndex - 1
|
||||
// Loop, exit when the search interval is empty (empty when i > j)
|
||||
while i <= j {
|
||||
let 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
|
||||
var i = nums.startIndex
|
||||
var j = nums.endIndex
|
||||
// Loop, exit when the search interval is empty (empty when i = j)
|
||||
while i < j {
|
||||
let 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
|
||||
}
|
||||
|
||||
@main
|
||||
enum BinarySearch {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let target = 6
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
var index = binarySearch(nums: nums, target: target)
|
||||
print("Index of target element 6 = \(index)")
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
index = binarySearchLCRO(nums: nums, target: target)
|
||||
print("Index of target element 6 = \(index)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: binary_search_edge.swift
|
||||
* Created Time: 2023-08-06
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import binary_search_insertion_target
|
||||
|
||||
/* Binary search for the leftmost target */
|
||||
func binarySearchLeftEdge(nums: [Int], target: Int) -> Int {
|
||||
// Equivalent to finding the insertion point of target
|
||||
let i = binarySearchInsertion(nums: nums, target: target)
|
||||
// Target not found, return -1
|
||||
if i == nums.endIndex || 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
|
||||
let i = binarySearchInsertion(nums: nums, target: target + 1)
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
let j = i - 1
|
||||
// Target not found, return -1
|
||||
if j == -1 || nums[j] != target {
|
||||
return -1
|
||||
}
|
||||
// Found target, return index j
|
||||
return j
|
||||
}
|
||||
|
||||
@main
|
||||
enum BinarySearchEdge {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// Array with duplicate elements
|
||||
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
|
||||
print("\nArray nums = \(nums)")
|
||||
|
||||
// Binary search left and right boundaries
|
||||
for target in [6, 7] {
|
||||
var index = binarySearchLeftEdge(nums: nums, target: target)
|
||||
print("Leftmost element \(target) index is \(index)")
|
||||
index = binarySearchRightEdge(nums: nums, target: target)
|
||||
print("Rightmost element \(target) index is \(index)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* File: binary_search_insertion.swift
|
||||
* Created Time: 2023-08-06
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
func binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {
|
||||
// Initialize closed interval [0, n-1]
|
||||
var i = nums.startIndex
|
||||
var j = nums.endIndex - 1
|
||||
while i <= j {
|
||||
let 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 func binarySearchInsertion(nums: [Int], target: Int) -> Int {
|
||||
// Initialize closed interval [0, n-1]
|
||||
var i = nums.startIndex
|
||||
var j = nums.endIndex - 1
|
||||
while i <= j {
|
||||
let 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
|
||||
}
|
||||
|
||||
#if !TARGET
|
||||
|
||||
@main
|
||||
enum BinarySearchInsertion {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// Array without duplicate elements
|
||||
var nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
|
||||
print("\nArray nums = \(nums)")
|
||||
// Binary search for insertion point
|
||||
for target in [6, 9] {
|
||||
let index = binarySearchInsertionSimple(nums: nums, target: target)
|
||||
print("Insertion point index for element \(target) is \(index)")
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
|
||||
print("\nArray nums = \(nums)")
|
||||
// Binary search for insertion point
|
||||
for target in [2, 6, 20] {
|
||||
let index = binarySearchInsertion(nums: nums, target: target)
|
||||
print("Insertion point index for element \(target) is \(index)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* File: binary_search_insertion.swift
|
||||
* Created Time: 2023-08-06
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
func binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {
|
||||
// Initialize closed interval [0, n-1]
|
||||
var i = nums.startIndex
|
||||
var j = nums.endIndex - 1
|
||||
while i <= j {
|
||||
let 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 func binarySearchInsertion(nums: [Int], target: Int) -> Int {
|
||||
// Initialize closed interval [0, n-1]
|
||||
var i = nums.startIndex
|
||||
var j = nums.endIndex - 1
|
||||
while i <= j {
|
||||
let 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
|
||||
}
|
||||
|
||||
#if !TARGET
|
||||
|
||||
@main
|
||||
enum BinarySearchInsertion {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// Array without duplicate elements
|
||||
var nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
|
||||
print("\nArray nums = \(nums)")
|
||||
// Binary search for insertion point
|
||||
for target in [6, 9] {
|
||||
let index = binarySearchInsertionSimple(nums: nums, target: target)
|
||||
print("Insertion point index for element \(target) is \(index)")
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
|
||||
print("\nArray nums = \(nums)")
|
||||
// Binary search for insertion point
|
||||
for target in [2, 6, 20] {
|
||||
let index = binarySearchInsertion(nums: nums, target: target)
|
||||
print("Insertion point index for element \(target) is \(index)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: hashing_search.swift
|
||||
* Created Time: 2023-01-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Hash search (array) */
|
||||
func hashingSearchArray(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
|
||||
return map[target, default: -1]
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? {
|
||||
// Hash table key: target node value, value: node object
|
||||
// If key is not in hash table, return null
|
||||
return map[target]
|
||||
}
|
||||
|
||||
@main
|
||||
enum HashingSearch {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let target = 3
|
||||
|
||||
/* Hash search (array) */
|
||||
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]
|
||||
// Initialize hash table
|
||||
var map: [Int: Int] = [:]
|
||||
for i in nums.indices {
|
||||
map[nums[i]] = i // key: element, value: index
|
||||
}
|
||||
let index = hashingSearchArray(map: map, target: target)
|
||||
print("Index of target element 3 = \(index)")
|
||||
|
||||
/* Hash search (linked list) */
|
||||
var head = ListNode.arrToLinkedList(arr: nums)
|
||||
// Initialize hash table
|
||||
var map1: [Int: ListNode] = [:]
|
||||
while head != nil {
|
||||
map1[head!.val] = head! // key: node value, value: node
|
||||
head = head?.next
|
||||
}
|
||||
let node = hashingSearchLinkedList(map: map1, target: target)
|
||||
print("Node object corresponding to target node value 3 is \(node!)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: linear_search.swift
|
||||
* Created Time: 2023-01-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Linear search (array) */
|
||||
func linearSearchArray(nums: [Int], target: Int) -> Int {
|
||||
// Traverse array
|
||||
for i in nums.indices {
|
||||
// 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(head: ListNode?, target: Int) -> ListNode? {
|
||||
var head = head
|
||||
// Traverse the linked list
|
||||
while head != nil {
|
||||
// Found the target node, return it
|
||||
if head?.val == target {
|
||||
return head
|
||||
}
|
||||
head = head?.next
|
||||
}
|
||||
// Target node not found, return null
|
||||
return nil
|
||||
}
|
||||
|
||||
@main
|
||||
enum LinearSearch {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let target = 3
|
||||
|
||||
/* Perform linear search in array */
|
||||
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]
|
||||
let index = linearSearchArray(nums: nums, target: target)
|
||||
print("Index of target element 3 = \(index)")
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
let head = ListNode.arrToLinkedList(arr: nums)
|
||||
let node = linearSearchLinkedList(head: head, target: target)
|
||||
print("Node object corresponding to target node value 3 is \(node!)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: two_sum.swift
|
||||
* Created Time: 2023-01-03
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Method 1: Brute force enumeration */
|
||||
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for i in nums.indices.dropLast() {
|
||||
for j in nums.indices.dropFirst(i + 1) {
|
||||
if nums[i] + nums[j] == target {
|
||||
return [i, j]
|
||||
}
|
||||
}
|
||||
}
|
||||
return [0]
|
||||
}
|
||||
|
||||
/* Method 2: Auxiliary hash table */
|
||||
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
var dic: [Int: Int] = [:]
|
||||
// Single loop, time complexity is O(n)
|
||||
for i in nums.indices {
|
||||
if let j = dic[target - nums[i]] {
|
||||
return [j, i]
|
||||
}
|
||||
dic[nums[i]] = i
|
||||
}
|
||||
return [0]
|
||||
}
|
||||
|
||||
@main
|
||||
enum LeetcodeTwoSum {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// ======= Test Case =======
|
||||
let nums = [2, 7, 11, 15]
|
||||
let target = 13
|
||||
// ====== Driver Code ======
|
||||
// Method 1
|
||||
var res = twoSumBruteForce(nums: nums, target: target)
|
||||
print("Method 1 res = \(res)")
|
||||
// Method 2
|
||||
res = twoSumHashTable(nums: nums, target: target)
|
||||
print("Method 2 res = \(res)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user