mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 06:56:06 +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:
@@ -11,38 +11,38 @@ import java.util.*;
|
||||
|
||||
public class heap {
|
||||
public static void testPush(Queue<Integer> heap, int val) {
|
||||
heap.offer(val); // Push the element into heap
|
||||
System.out.format("\nAfter element %d is added to the heap\n", val);
|
||||
heap.offer(val); // Element enters heap
|
||||
System.out.format("\nAfter element %d enters heap\n", val);
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public static void testPop(Queue<Integer> heap) {
|
||||
int val = heap.poll(); // Pop the element at the heap top
|
||||
System.out.format("\nAfter the top element %d is removed from the heap\n", val);
|
||||
int val = heap.poll(); // Time complexity is O(n), not O(nlogn)
|
||||
System.out.format("\nAfter heap top element %d exits heap\n", val);
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize the heap */
|
||||
// Initialize min-heap
|
||||
/* Initialize heap */
|
||||
// Python's heapq module implements min heap by default
|
||||
Queue<Integer> minHeap = new PriorityQueue<>();
|
||||
// Initialize the max-heap (using lambda expression to modify Comparator if necessary)
|
||||
// Initialize max heap (modify Comparator using lambda expression)
|
||||
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
||||
|
||||
System.out.println("\nThe following test case is for max-heap");
|
||||
System.out.println("\nThe following test cases are for max heap");
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
testPush(maxHeap, 1);
|
||||
testPush(maxHeap, 3);
|
||||
testPush(maxHeap, 2);
|
||||
testPush(maxHeap, 5);
|
||||
testPush(maxHeap, 4);
|
||||
|
||||
/* Access heap top element */
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.peek();
|
||||
System.out.format("\nTop element of the heap is %d\n", peek);
|
||||
System.out.format("\nHeap top element is %d\n", peek);
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
@@ -51,16 +51,16 @@ public class heap {
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
System.out.format("\nNumber of elements in the heap is %d\n", size);
|
||||
System.out.format("\nHeap element count is %d\n", size);
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
System.out.format("\nIs the heap empty %b\n", isEmpty);
|
||||
System.out.format("\nHeap is empty %b\n", isEmpty);
|
||||
|
||||
/* Enter list and build heap */
|
||||
/* Input list and build heap */
|
||||
// Time complexity is O(n), not O(nlogn)
|
||||
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
|
||||
System.out.println("\nEnter list and build min-heap");
|
||||
System.out.println("\nAfter inputting list and building min heap");
|
||||
PrintUtil.printHeap(minHeap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,16 @@ package chapter_heap;
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* Max-heap */
|
||||
/* Max heap */
|
||||
class MaxHeap {
|
||||
// Use list instead of array to avoid the need for resizing
|
||||
// Use list instead of array, no need to consider capacity expansion
|
||||
private List<Integer> maxHeap;
|
||||
|
||||
/* Constructor, build heap based on input list */
|
||||
public MaxHeap(List<Integer> nums) {
|
||||
// Add all list elements into the heap
|
||||
// Add list elements to heap as is
|
||||
maxHeap = new ArrayList<>(nums);
|
||||
// Heapify all nodes except leaves
|
||||
// Heapify all nodes except leaf nodes
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
@@ -36,7 +36,7 @@ class MaxHeap {
|
||||
|
||||
/* Get index of parent node */
|
||||
private int parent(int i) {
|
||||
return (i - 1) / 2; // Integer division down
|
||||
return (i - 1) / 2; // Floor division
|
||||
}
|
||||
|
||||
/* Swap elements */
|
||||
@@ -51,17 +51,17 @@ class MaxHeap {
|
||||
return maxHeap.size();
|
||||
}
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Access heap top element */
|
||||
/* Access top element */
|
||||
public int peek() {
|
||||
return maxHeap.get(0);
|
||||
}
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
public void push(int val) {
|
||||
// Add node
|
||||
maxHeap.add(val);
|
||||
@@ -69,46 +69,46 @@ class MaxHeap {
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from bottom to top */
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
private void siftUp(int i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
int p = parent(i);
|
||||
// When "crossing the root node" or "node does not need repair", end heapification
|
||||
// When "crossing root node" or "node needs no repair", end heapify
|
||||
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
|
||||
break;
|
||||
// Swap two nodes
|
||||
swap(i, p);
|
||||
// Loop upwards heapification
|
||||
// Loop upward heapify
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
public int pop() {
|
||||
// Empty handling
|
||||
// Handle empty case
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
// Delete node
|
||||
swap(0, size() - 1);
|
||||
// Remove node
|
||||
int val = maxHeap.remove(size() - 1);
|
||||
// Heapify from top to bottom
|
||||
// Return top element
|
||||
siftDown(0);
|
||||
// Return heap top element
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from top to bottom */
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
private void siftDown(int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as ma
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
int l = left(i), r = right(i), ma = i;
|
||||
if (l < size() && maxHeap.get(l) > maxHeap.get(ma))
|
||||
ma = l;
|
||||
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
|
||||
ma = r;
|
||||
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
// Swap two nodes
|
||||
if (ma == i)
|
||||
break;
|
||||
// Swap two nodes
|
||||
@@ -118,7 +118,7 @@ class MaxHeap {
|
||||
}
|
||||
}
|
||||
|
||||
/* Print heap (binary tree) */
|
||||
/* Driver Code */
|
||||
public void print() {
|
||||
Queue<Integer> queue = new PriorityQueue<>((a, b) -> { return b - a; });
|
||||
queue.addAll(maxHeap);
|
||||
@@ -128,32 +128,32 @@ class MaxHeap {
|
||||
|
||||
public class my_heap {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize max-heap */
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
MaxHeap maxHeap = new MaxHeap(Arrays.asList(9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2));
|
||||
System.out.println("\nEnter list and build heap");
|
||||
System.out.println("\nAfter inputting list and building heap");
|
||||
maxHeap.print();
|
||||
|
||||
/* Access heap top element */
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.peek();
|
||||
System.out.format("\nTop element of the heap is %d\n", peek);
|
||||
System.out.format("\nHeap top element is %d\n", peek);
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
System.out.format("\nAfter element %d is added to the heap\n", val);
|
||||
System.out.format("\nAfter element %d enters heap\n", val);
|
||||
maxHeap.print();
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
peek = maxHeap.pop();
|
||||
System.out.format("\nAfter the top element %d is removed from the heap\n", peek);
|
||||
System.out.format("\nAfter heap top element %d exits heap\n", peek);
|
||||
maxHeap.print();
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
System.out.format("\nNumber of elements in the heap is %d\n", size);
|
||||
System.out.format("\nHeap element count is %d\n", size);
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
System.out.format("\nIs the heap empty %b\n", isEmpty);
|
||||
System.out.format("\nHeap is empty %b\n", isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,17 +10,17 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class top_k {
|
||||
/* Using heap to find the largest k elements in an array */
|
||||
/* Find the largest k elements in array based on heap */
|
||||
static Queue<Integer> topKHeap(int[] nums, int k) {
|
||||
// Initialize min-heap
|
||||
// Python's heapq module implements min heap by default
|
||||
Queue<Integer> heap = new PriorityQueue<Integer>();
|
||||
// Enter the first k elements of the array into the heap
|
||||
// Enter the first k elements of array into heap
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.offer(nums[i]);
|
||||
}
|
||||
// From the k+1th element, keep the heap length as k
|
||||
// Starting from the (k+1)th element, maintain heap length as k
|
||||
for (int i = k; i < nums.length; i++) {
|
||||
// If the current element is larger than the heap top element, remove the heap top element and enter the current element into the heap
|
||||
// If current element is greater than top element, top element exits heap, current element enters heap
|
||||
if (nums[i] > heap.peek()) {
|
||||
heap.poll();
|
||||
heap.offer(nums[i]);
|
||||
|
||||
Reference in New Issue
Block a user