translation: Add Python and Java code for EN version (#1345)

* Add the intial translation of code of all the languages

* test

* revert

* Remove

* Add Python and Java code for EN version
This commit is contained in:
Yudong Jin
2024-05-06 05:21:51 +08:00
committed by GitHub
parent b5e198db7d
commit 1c0f350ad6
174 changed files with 12349 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
/**
* File: binary_search_recur.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
public class binary_search_recur {
/* Binary search: problem f(i, j) */
static int dfs(int[] nums, int target, int i, int j) {
// If the interval is empty, indicating no target element, return -1
if (i > j) {
return -1;
}
// Calculate midpoint index m
int m = (i + j) / 2;
if (nums[m] < target) {
// Recursive subproblem f(m+1, j)
return dfs(nums, target, m + 1, j);
} else if (nums[m] > target) {
// Recursive subproblem f(i, m-1)
return dfs(nums, target, i, m - 1);
} else {
// Found the target element, thus return its index
return m;
}
}
/* Binary search */
static int binarySearch(int[] nums, int target) {
int n = nums.length;
// Solve problem f(0, n-1)
return dfs(nums, target, 0, n - 1);
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
// Binary search (double closed interval)
int index = binarySearch(nums, target);
System.out.println("Index of target element 6 =" + index);
}
}

View File

@@ -0,0 +1,51 @@
/**
* File: build_tree.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
import utils.*;
import java.util.*;
public class build_tree {
/* Build binary tree: Divide and conquer */
static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
// Terminate when subtree interval is empty
if (r - l < 0)
return null;
// Initialize root node
TreeNode root = new TreeNode(preorder[i]);
// Query m to divide left and right subtrees
int m = inorderMap.get(preorder[i]);
// Subproblem: build left subtree
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// Subproblem: build right subtree
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// Return root node
return root;
}
/* Build binary tree */
static TreeNode buildTree(int[] preorder, int[] inorder) {
// Initialize hash table, storing in-order elements to indices mapping
Map<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
inorderMap.put(inorder[i], i);
}
TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root;
}
public static void main(String[] args) {
int[] preorder = { 3, 9, 2, 1, 7 };
int[] inorder = { 9, 3, 1, 2, 7 };
System.out.println("Pre-order traversal = " + Arrays.toString(preorder));
System.out.println("In-order traversal = " + Arrays.toString(inorder));
TreeNode root = buildTree(preorder, inorder);
System.out.println("The built binary tree is:");
PrintUtil.printTree(root);
}
}

View File

@@ -0,0 +1,59 @@
/**
* File: hanota.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
import java.util.*;
public class hanota {
/* Move a disc */
static void move(List<Integer> src, List<Integer> tar) {
// Take out a disc from the top of src
Integer pan = src.remove(src.size() - 1);
// Place the disc on top of tar
tar.add(pan);
}
/* Solve the Tower of Hanoi problem f(i) */
static void dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {
// If only one disc remains on src, move it to tar
if (i == 1) {
move(src, tar);
return;
}
// Subproblem f(i-1): move the top i-1 discs from src with the help of tar to buf
dfs(i - 1, src, tar, buf);
// Subproblem f(1): move the remaining one disc from src to tar
move(src, tar);
// Subproblem f(i-1): move the top i-1 discs from buf with the help of src to tar
dfs(i - 1, buf, src, tar);
}
/* Solve the Tower of Hanoi problem */
static void solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {
int n = A.size();
// Move the top n discs from A with the help of B to C
dfs(n, A, B, C);
}
public static void main(String[] args) {
// The tail of the list is the top of the pillar
List<Integer> A = new ArrayList<>(Arrays.asList(5, 4, 3, 2, 1));
List<Integer> B = new ArrayList<>();
List<Integer> C = new ArrayList<>();
System.out.println("Initial state:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
solveHanota(A, B, C);
System.out.println("After the discs are moved:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
}
}