mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-08 05:01:01 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
28
ja/codes/java/utils/ListNode.java
Normal file
28
ja/codes/java/utils/ListNode.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* File: ListNode.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package utils;
|
||||
|
||||
/* 連結リストノード */
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/* リストを連結リストにデシリアライズ */
|
||||
public static ListNode arrToLinkedList(int[] arr) {
|
||||
ListNode dum = new ListNode(0);
|
||||
ListNode head = dum;
|
||||
for (int val : arr) {
|
||||
head.next = new ListNode(val);
|
||||
head = head.next;
|
||||
}
|
||||
return dum.next;
|
||||
}
|
||||
}
|
||||
116
ja/codes/java/utils/PrintUtil.java
Normal file
116
ja/codes/java/utils/PrintUtil.java
Normal file
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* File: PrintUtil.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Trunk {
|
||||
Trunk prev;
|
||||
String str;
|
||||
|
||||
Trunk(Trunk prev, String str) {
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
public class PrintUtil {
|
||||
/* 行列を印刷 (配列) */
|
||||
public static <T> void printMatrix(T[][] matrix) {
|
||||
System.out.println("[");
|
||||
for (T[] row : matrix) {
|
||||
System.out.println(" " + row + ",");
|
||||
}
|
||||
System.out.println("]");
|
||||
}
|
||||
|
||||
/* 行列を印刷 (リスト) */
|
||||
public static <T> void printMatrix(List<List<T>> matrix) {
|
||||
System.out.println("[");
|
||||
for (List<T> row : matrix) {
|
||||
System.out.println(" " + row + ",");
|
||||
}
|
||||
System.out.println("]");
|
||||
}
|
||||
|
||||
/* 連結リストを印刷 */
|
||||
public static void printLinkedList(ListNode head) {
|
||||
List<String> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
list.add(String.valueOf(head.val));
|
||||
head = head.next;
|
||||
}
|
||||
System.out.println(String.join(" -> ", list));
|
||||
}
|
||||
|
||||
/* 二分木を印刷 */
|
||||
public static void printTree(TreeNode root) {
|
||||
printTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 二分木を印刷
|
||||
* この木プリンターはTECHIE DELIGHTから借用
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
public static void printTree(TreeNode root, Trunk prev, boolean isRight) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String prev_str = " ";
|
||||
Trunk trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = "———";
|
||||
} else if (isRight) {
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
} else {
|
||||
trunk.str = "\\———";
|
||||
prev.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
System.out.println(" " + root.val);
|
||||
|
||||
if (prev != null) {
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = " |";
|
||||
|
||||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
public static void showTrunks(Trunk p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
System.out.print(p.str);
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
public static <K, V> void printHashMap(Map<K, V> map) {
|
||||
for (Map.Entry<K, V> kv : map.entrySet()) {
|
||||
System.out.println(kv.getKey() + " -> " + kv.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/* ヒープを印刷 (優先度キュー) */
|
||||
public static void printHeap(Queue<Integer> queue) {
|
||||
List<Integer> list = new ArrayList<>(queue);
|
||||
System.out.print("ヒープの配列表現:");
|
||||
System.out.println(list);
|
||||
System.out.println("ヒープの木表現:");
|
||||
TreeNode root = TreeNode.listToTree(list);
|
||||
printTree(root);
|
||||
}
|
||||
}
|
||||
73
ja/codes/java/utils/TreeNode.java
Normal file
73
ja/codes/java/utils/TreeNode.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* File: TreeNode.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 二分木ノードクラス */
|
||||
public class TreeNode {
|
||||
public int val; // ノード値
|
||||
public int height; // ノード高さ
|
||||
public TreeNode left; // 左子ノードへの参照
|
||||
public TreeNode right; // 右子ノードへの参照
|
||||
|
||||
/* コンストラクタ */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
// シリアライゼーション符号化ルールについては、次を参照:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// 二分木の配列表現:
|
||||
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
// 二分木の連結リスト表現:
|
||||
// /——— 15
|
||||
// /——— 7
|
||||
// /——— 3
|
||||
// | \——— 6
|
||||
// | \——— 12
|
||||
// ——— 1
|
||||
// \——— 2
|
||||
// | /——— 9
|
||||
// \——— 4
|
||||
// \——— 8
|
||||
|
||||
/* リストを二分木にデシリアライズ:再帰的 */
|
||||
private static TreeNode listToTreeDFS(List<Integer> arr, int i) {
|
||||
if (i < 0 || i >= arr.size() || arr.get(i) == null) {
|
||||
return null;
|
||||
}
|
||||
TreeNode root = new TreeNode(arr.get(i));
|
||||
root.left = listToTreeDFS(arr, 2 * i + 1);
|
||||
root.right = listToTreeDFS(arr, 2 * i + 2);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* リストを二分木にデシリアライズ */
|
||||
public static TreeNode listToTree(List<Integer> arr) {
|
||||
return listToTreeDFS(arr, 0);
|
||||
}
|
||||
|
||||
/* 二分木をリストにシリアライズ:再帰的 */
|
||||
private static void treeToListDFS(TreeNode root, int i, List<Integer> res) {
|
||||
if (root == null)
|
||||
return;
|
||||
while (i >= res.size()) {
|
||||
res.add(null);
|
||||
}
|
||||
res.set(i, root.val);
|
||||
treeToListDFS(root.left, 2 * i + 1, res);
|
||||
treeToListDFS(root.right, 2 * i + 2, res);
|
||||
}
|
||||
|
||||
/* 二分木をリストにシリアライズ */
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
36
ja/codes/java/utils/Vertex.java
Normal file
36
ja/codes/java/utils/Vertex.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* File: Vertex.java
|
||||
* Created Time: 2023-02-15
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 頂点クラス */
|
||||
public class Vertex {
|
||||
public int val;
|
||||
|
||||
public Vertex(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
/* 値のリストvalsを入力し、頂点のリストvetsを返す */
|
||||
public static Vertex[] valsToVets(int[] vals) {
|
||||
Vertex[] vets = new Vertex[vals.length];
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
vets[i] = new Vertex(vals[i]);
|
||||
}
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* 頂点のリストvetsを入力し、値のリストvalsを返す */
|
||||
public static List<Integer> vetsToVals(List<Vertex> vets) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (Vertex vet : vets) {
|
||||
vals.add(vet.val);
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user