mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-01 12:47:14 +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,65 @@
|
||||
/*
|
||||
* File: binary_search.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
fn binary_search(nums: &[i32], target: i32) -> i32 {
|
||||
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
|
||||
let mut i = 0;
|
||||
let mut j = nums.len() as i32 - 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 as usize] < target {
|
||||
// This means target is in the interval [m+1, j]
|
||||
i = m + 1;
|
||||
} else if nums[m as usize] > 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) */
|
||||
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
|
||||
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
|
||||
let mut i = 0;
|
||||
let mut j = nums.len() as i32;
|
||||
// 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 as usize] < target {
|
||||
// This means target is in the interval [m+1, j)
|
||||
i = m + 1;
|
||||
} else if nums[m as usize] > 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;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 6;
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
|
||||
// Binary search (closed interval on both sides)
|
||||
let mut index = binary_search(&nums, target);
|
||||
println!("Index of target element 6 is {index}");
|
||||
|
||||
// Binary search (left-closed right-open interval)
|
||||
index = binary_search_lcro(&nums, target);
|
||||
println!("Index of target element 6 is {index}");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: binary_search_edge.rs
|
||||
* Created Time: 2023-08-30
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
mod binary_search_insertion;
|
||||
|
||||
use binary_search_insertion::binary_search_insertion;
|
||||
|
||||
/* Binary search for the leftmost target */
|
||||
fn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {
|
||||
// Equivalent to finding the insertion point of target
|
||||
let i = binary_search_insertion(nums, target);
|
||||
// Target not found, return -1
|
||||
if i == nums.len() as i32 || nums[i as usize] != target {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index i
|
||||
i
|
||||
}
|
||||
|
||||
/* Binary search for the rightmost target */
|
||||
fn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {
|
||||
// Convert to finding the leftmost target + 1
|
||||
let i = binary_search_insertion(nums, 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 as usize] != target {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index j
|
||||
j
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// Array with duplicate elements
|
||||
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
println!("\nArray nums = {:?}", nums);
|
||||
|
||||
// Binary search left and right boundaries
|
||||
for target in [6, 7] {
|
||||
let index = binary_search_left_edge(&nums, target);
|
||||
println!("Leftmost element {} index is {}", target, index);
|
||||
let index = binary_search_right_edge(&nums, target);
|
||||
println!("Rightmost element {} index is {}", target, index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* File: binary_search_insertion.rs
|
||||
* Created Time: 2023-08-30
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
#![allow(unused)]
|
||||
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
fn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {
|
||||
let (mut i, mut j) = (0, nums.len() as i32 - 1); // Initialize closed interval [0, n-1]
|
||||
while i <= j {
|
||||
let m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if nums[m as usize] < target {
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
} else if nums[m as usize] > target {
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
} else {
|
||||
return m;
|
||||
}
|
||||
}
|
||||
// Target not found, return insertion point i
|
||||
i
|
||||
}
|
||||
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
pub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {
|
||||
let (mut i, mut j) = (0, nums.len() as i32 - 1); // Initialize closed interval [0, n-1]
|
||||
while i <= j {
|
||||
let m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if nums[m as usize] < target {
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
} else if nums[m as usize] > 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
|
||||
i
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// Array without duplicate elements
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
println!("\nArray nums = {:?}", nums);
|
||||
// Binary search for insertion point
|
||||
for target in [6, 9] {
|
||||
let index = binary_search_insertion_simple(&nums, target);
|
||||
println!("Insertion point index for element {} is {}", target, index);
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
println!("\nArray nums = {:?}", nums);
|
||||
// Binary search for insertion point
|
||||
for target in [2, 6, 20] {
|
||||
let index = binary_search_insertion(&nums, target);
|
||||
println!("Insertion point index for element {} is {}", target, index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: hashing_search.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::ListNode;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
/* Hash search (array) */
|
||||
fn hashing_search_array<'a>(map: &'a HashMap<i32, usize>, target: i32) -> Option<&'a usize> {
|
||||
// Hash table's key: target element, value: index
|
||||
// If this key does not exist in the hash table, return None
|
||||
map.get(&target)
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
fn hashing_search_linked_list(
|
||||
map: &HashMap<i32, Rc<RefCell<ListNode<i32>>>>,
|
||||
target: i32,
|
||||
) -> Option<&Rc<RefCell<ListNode<i32>>>> {
|
||||
// Hash table key: target node value, value: node object
|
||||
// If this key does not exist in the hash table, return None
|
||||
map.get(&target)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 3;
|
||||
|
||||
/* Hash search (array) */
|
||||
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
// Initialize hash table
|
||||
let mut map = HashMap::new();
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
map.insert(*num, i); // key: element, value: index
|
||||
}
|
||||
let index = hashing_search_array(&map, target);
|
||||
println!("Index of target element 3 = {}", index.unwrap());
|
||||
|
||||
/* Hash search (linked list) */
|
||||
let head = ListNode::arr_to_linked_list(&nums);
|
||||
// Initialize hash table
|
||||
// let mut map1 = HashMap::new();
|
||||
let map1 = ListNode::linked_list_to_hashmap(head);
|
||||
let node = hashing_search_linked_list(&map1, target);
|
||||
println!("Node object corresponding to target node value 3 is {:?}", node);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* File: linear_search.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::ListNode;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/* Linear search (array) */
|
||||
fn linear_search_array(nums: &[i32], target: i32) -> i32 {
|
||||
// Traverse array
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
// Found the target element, return its index
|
||||
if num == &target {
|
||||
return i as i32;
|
||||
}
|
||||
}
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Linear search (linked list) */
|
||||
fn linear_search_linked_list(
|
||||
head: Rc<RefCell<ListNode<i32>>>,
|
||||
target: i32,
|
||||
) -> Option<Rc<RefCell<ListNode<i32>>>> {
|
||||
// Found the target node, return it
|
||||
if head.borrow().val == target {
|
||||
return Some(head);
|
||||
};
|
||||
// Found the target node, return it
|
||||
if let Some(node) = &head.borrow_mut().next {
|
||||
return linear_search_linked_list(node.clone(), target);
|
||||
}
|
||||
// Target node not found, return None
|
||||
return None;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 3;
|
||||
|
||||
/* Perform linear search in array */
|
||||
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
let index = linear_search_array(&nums, target);
|
||||
println!("Index of target element 3 = {}", index);
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
let head = ListNode::arr_to_linked_list(&nums);
|
||||
let node = linear_search_linked_list(head.unwrap(), target);
|
||||
println!("Node object corresponding to target node value 3 is {:?}", node);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* File: two_sum.rs
|
||||
* Created Time: 2023-01-14
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/* Method 1: Brute force enumeration */
|
||||
pub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
|
||||
let size = nums.len();
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for i in 0..size - 1 {
|
||||
for j in i + 1..size {
|
||||
if nums[i] + nums[j] == target {
|
||||
return Some(vec![i as i32, j as i32]);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/* Method 2: Auxiliary hash table */
|
||||
pub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
let mut dic = HashMap::new();
|
||||
// Single loop, time complexity is O(n)
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
match dic.get(&(target - num)) {
|
||||
Some(v) => return Some(vec![*v as i32, i as i32]),
|
||||
None => dic.insert(num, i as i32),
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// ======= Test Case =======
|
||||
let nums = vec![2, 7, 11, 15];
|
||||
let target = 13;
|
||||
|
||||
// ====== Driver Code ======
|
||||
// Method 1
|
||||
let res = two_sum_brute_force(&nums, target).unwrap();
|
||||
print!("Method 1 res = ");
|
||||
print_util::print_array(&res);
|
||||
// Method 2
|
||||
let res = two_sum_hash_table(&nums, target).unwrap();
|
||||
print!("\nMethod 2 res = ");
|
||||
print_util::print_array(&res);
|
||||
}
|
||||
Reference in New Issue
Block a user