mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 07:26:07 +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,60 @@
|
||||
/**
|
||||
* File: binary_search.js
|
||||
* Created Time: 2022-12-22
|
||||
* Author: JoseHung (szhong@link.cuhk.edu.hk)
|
||||
*/
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
function binarySearch(nums, target) {
|
||||
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
|
||||
let i = 0,
|
||||
j = nums.length - 1;
|
||||
// Loop, exit when the search interval is empty (empty when i > j)
|
||||
while (i <= j) {
|
||||
// Calculate midpoint index m, use parseInt() to round down
|
||||
const m = parseInt(i + (j - i) / 2);
|
||||
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 return m; // Found the target element, return its index
|
||||
}
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
function binarySearchLCRO(nums, target) {
|
||||
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
|
||||
let i = 0,
|
||||
j = nums.length;
|
||||
// Loop, exit when the search interval is empty (empty when i = j)
|
||||
while (i < j) {
|
||||
// Calculate midpoint index m, use parseInt() to round down
|
||||
const m = parseInt(i + (j - i) / 2);
|
||||
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;
|
||||
// Found the target element, return its index
|
||||
else return m;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const target = 6;
|
||||
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
|
||||
/* Binary search (closed interval on both sides) */
|
||||
let index = binarySearch(nums, target);
|
||||
console.log('Index of target element 6 = ' + index);
|
||||
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
index = binarySearchLCRO(nums, target);
|
||||
console.log('Index of target element 6 = ' + index);
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: binary_search_edge.js
|
||||
* Created Time: 2023-08-22
|
||||
* Author: Gaofer Chou (gaofer-chou@qq.com)
|
||||
*/
|
||||
|
||||
const { binarySearchInsertion } = require('./binary_search_insertion.js');
|
||||
|
||||
/* Binary search for the leftmost target */
|
||||
function binarySearchLeftEdge(nums, target) {
|
||||
// Equivalent to finding the insertion point of target
|
||||
const i = 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 */
|
||||
function binarySearchRightEdge(nums, target) {
|
||||
// Convert to finding the leftmost target + 1
|
||||
const i = binarySearchInsertion(nums, target + 1);
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
const j = i - 1;
|
||||
// Target not found, return -1
|
||||
if (j === -1 || nums[j] !== target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index j
|
||||
return j;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// Array with duplicate elements
|
||||
const nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
console.log('\nArray nums = ' + nums);
|
||||
// Binary search left and right boundaries
|
||||
for (const target of [6, 7]) {
|
||||
let index = binarySearchLeftEdge(nums, target);
|
||||
console.log('Leftmost element ' + target + ' has index ' + index);
|
||||
index = binarySearchRightEdge(nums, target);
|
||||
console.log('Rightmost element ' + target + ' has index ' + index);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* File: binary_search_insertion.js
|
||||
* Created Time: 2023-08-22
|
||||
* Author: Gaofer Chou (gaofer-chou@qq.com)
|
||||
*/
|
||||
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
function binarySearchInsertionSimple(nums, target) {
|
||||
let i = 0,
|
||||
j = nums.length - 1; // Initialize closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down
|
||||
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) */
|
||||
function binarySearchInsertion(nums, target) {
|
||||
let i = 0,
|
||||
j = nums.length - 1; // Initialize closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
const m = Math.floor(i + (j - i) / 2); // Calculate midpoint index m, use Math.floor() to round down
|
||||
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;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// Array without duplicate elements
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
console.log('\nArray nums = ' + nums);
|
||||
// Binary search for insertion point
|
||||
for (const target of [6, 9]) {
|
||||
const index = binarySearchInsertionSimple(nums, target);
|
||||
console.log('Element ' + target + ''s insertion point index is ' + index);
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
console.log('\nArray nums = ' + nums);
|
||||
// Binary search for insertion point
|
||||
for (const target of [2, 6, 20]) {
|
||||
const index = binarySearchInsertion(nums, target);
|
||||
console.log('Element ' + target + ''s insertion point index is ' + index);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
binarySearchInsertion,
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: hashing_search.js
|
||||
* Created Time: 2022-12-29
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
const { arrToLinkedList } = require('../modules/ListNode');
|
||||
|
||||
/* Hash search (array) */
|
||||
function hashingSearchArray(map, target) {
|
||||
// Hash table's key: target element, value: index
|
||||
// If this key does not exist in the hash table, return -1
|
||||
return map.has(target) ? map.get(target) : -1;
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
function hashingSearchLinkedList(map, target) {
|
||||
// Hash table key: target node value, value: node object
|
||||
// If key is not in hash table, return null
|
||||
return map.has(target) ? map.get(target) : null;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const target = 3;
|
||||
|
||||
/* Hash search (array) */
|
||||
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
// Initialize hash table
|
||||
const map = new Map();
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
map.set(nums[i], i); // key: element, value: index
|
||||
}
|
||||
const index = hashingSearchArray(map, target);
|
||||
console.log('Index of target element 3 = ' + index);
|
||||
|
||||
/* Hash search (linked list) */
|
||||
let head = arrToLinkedList(nums);
|
||||
// Initialize hash table
|
||||
const map1 = new Map();
|
||||
while (head != null) {
|
||||
map1.set(head.val, head); // key: node value, value: node
|
||||
head = head.next;
|
||||
}
|
||||
const node = hashingSearchLinkedList(map1, target);
|
||||
console.log('Node object with target value 3 is', node);
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* File: linear_search.js
|
||||
* Created Time: 2022-12-22
|
||||
* Author: JoseHung (szhong@link.cuhk.edu.hk)
|
||||
*/
|
||||
|
||||
const { ListNode, arrToLinkedList } = require('../modules/ListNode');
|
||||
|
||||
/* Linear search (array) */
|
||||
function linearSearchArray(nums, target) {
|
||||
// Traverse array
|
||||
for (let 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) */
|
||||
function linearSearchLinkedList(head, target) {
|
||||
// Traverse the linked list
|
||||
while (head) {
|
||||
// Found the target node, return it
|
||||
if (head.val === target) {
|
||||
return head;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
// Target node not found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const target = 3;
|
||||
|
||||
/* Perform linear search in array */
|
||||
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
const index = linearSearchArray(nums, target);
|
||||
console.log('Index of target element 3 = ' + index);
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
const head = arrToLinkedList(nums);
|
||||
const node = linearSearchLinkedList(head, target);
|
||||
console.log('Node object corresponding to target node value 3 is ', node);
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: two_sum.js
|
||||
* Created Time: 2022-12-15
|
||||
* Author: gyt95 (gytkwan@gmail.com)
|
||||
*/
|
||||
|
||||
/* Method 1: Brute force enumeration */
|
||||
function twoSumBruteForce(nums, target) {
|
||||
const n = nums.length;
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
if (nums[i] + nums[j] === target) {
|
||||
return [i, j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/* Method 2: Auxiliary hash table */
|
||||
function twoSumHashTable(nums, target) {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
let m = {};
|
||||
// Single loop, time complexity is O(n)
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (m[target - nums[i]] !== undefined) {
|
||||
return [m[target - nums[i]], i];
|
||||
} else {
|
||||
m[nums[i]] = i;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// Method 1
|
||||
const nums = [2, 7, 11, 15],
|
||||
target = 13;
|
||||
|
||||
let res = twoSumBruteForce(nums, target);
|
||||
console.log('Method 1 res = ', res);
|
||||
|
||||
// Method 2
|
||||
res = twoSumHashTable(nums, target);
|
||||
console.log('Method 2 res = ', res);
|
||||
Reference in New Issue
Block a user