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:
Ikko Eltociear Ashimine
2025-10-17 06:04:43 +09:00
committed by GitHub
parent 2487a27036
commit 954c45864b
886 changed files with 33569 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
/**
* File: array_hash_map.java
* Created Time: 2022-12-04
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.*;
/* キー値ペア */
class Pair {
public int key;
public String val;
public Pair(int key, String val) {
this.key = key;
this.val = val;
}
}
/* 配列実装に基づくハッシュテーブル */
class ArrayHashMap {
private List<Pair> buckets;
public ArrayHashMap() {
// 100個のバケットを含む配列を初期化
buckets = new ArrayList<>();
for (int i = 0; i < 100; i++) {
buckets.add(null);
}
}
/* ハッシュ関数 */
private int hashFunc(int key) {
int index = key % 100;
return index;
}
/* クエリ操作 */
public String get(int key) {
int index = hashFunc(key);
Pair pair = buckets.get(index);
if (pair == null)
return null;
return pair.val;
}
/* 追加操作 */
public void put(int key, String val) {
Pair pair = new Pair(key, val);
int index = hashFunc(key);
buckets.set(index, pair);
}
/* 削除操作 */
public void remove(int key) {
int index = hashFunc(key);
// nullに設定して削除を示す
buckets.set(index, null);
}
/* すべてのキー値ペアを取得 */
public List<Pair> pairSet() {
List<Pair> pairSet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
pairSet.add(pair);
}
return pairSet;
}
/* すべてのキーを取得 */
public List<Integer> keySet() {
List<Integer> keySet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
keySet.add(pair.key);
}
return keySet;
}
/* すべての値を取得 */
public List<String> valueSet() {
List<String> valueSet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
valueSet.add(pair.val);
}
return valueSet;
}
/* ハッシュテーブルを印刷 */
public void print() {
for (Pair kv : pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
}
}
public class array_hash_map {
public static void main(String[] args) {
/* ハッシュテーブルを初期化 */
ArrayHashMap map = new ArrayHashMap();
/* 追加操作 */
// ハッシュテーブルにキー値ペア (key, value) を追加
map.put(12836, "Ha");
map.put(15937, "Luo");
map.put(16750, "Suan");
map.put(13276, "Fa");
map.put(10583, "Ya");
System.out.println("\n追加後のハッシュテーブル\nKey -> Value");
map.print();
/* クエリ操作 */
// ハッシュテーブルにキーを入力して値を取得
String name = map.get(15937);
System.out.println("\n学生ID 15937を入力、名前 " + name + " を見つけました");
/* 削除操作 */
// ハッシュテーブルからキー値ペア (key, value) を削除
map.remove(10583);
System.out.println("\n10583を削除後のハッシュテーブル\nKey -> Value");
map.print();
/* ハッシュテーブルを走査 */
System.out.println("\nキー値ペアを走査 Key->Value");
for (Pair kv : map.pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
System.out.println("\nキーを個別に走査 Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\n値を個別に走査 Value");
for (String val : map.valueSet()) {
System.out.println(val);
}
}
}

View File

@@ -0,0 +1,38 @@
/**
* File: built_in_hash.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import utils.*;
import java.util.*;
public class built_in_hash {
public static void main(String[] args) {
int num = 3;
int hashNum = Integer.hashCode(num);
System.out.println("整数 " + num + " のハッシュ値は " + hashNum + " です");
boolean bol = true;
int hashBol = Boolean.hashCode(bol);
System.out.println("ブール値 " + bol + " のハッシュ値は " + hashBol + " です");
double dec = 3.14159;
int hashDec = Double.hashCode(dec);
System.out.println("小数 " + dec + " のハッシュ値は " + hashDec + " です");
String str = "Hello algorithm";
int hashStr = str.hashCode();
System.out.println("文字列 " + str + " のハッシュ値は " + hashStr + " です");
Object[] arr = { 12836, "Ha" };
int hashTup = Arrays.hashCode(arr);
System.out.println("配列 " + Arrays.toString(arr) + " のハッシュ値は " + hashTup + " です");
ListNode obj = new ListNode(0);
int hashObj = obj.hashCode();
System.out.println("ノードオブジェクト " + obj + " のハッシュ値は " + hashObj + " です");
}
}

View File

@@ -0,0 +1,52 @@
/**
* File: hash_map.java
* Created Time: 2022-12-04
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.*;
import utils.*;
public class hash_map {
public static void main(String[] args) {
/* ハッシュテーブルを初期化 */
Map<Integer, String> map = new HashMap<>();
/* 追加操作 */
// ハッシュテーブルにキー値ペア (key, value) を追加
map.put(12836, "Ha");
map.put(15937, "Luo");
map.put(16750, "Suan");
map.put(13276, "Fa");
map.put(10583, "Ya");
System.out.println("\n追加後、ハッシュテーブルは\nKey -> Value");
PrintUtil.printHashMap(map);
/* 検索操作 */
// ハッシュテーブルにキーを入力し、値を取得
String name = map.get(15937);
System.out.println("\n学生番号 15937 を入力し、名前 " + name + " を見つけました");
/* 削除操作 */
// ハッシュテーブルからキー値ペア (key, value) を削除
map.remove(10583);
System.out.println("\n10583 を削除後、ハッシュテーブルは\nKey -> Value");
PrintUtil.printHashMap(map);
/* ハッシュテーブルの走査 */
System.out.println("\nキー値ペアを走査 Key->Value");
for (Map.Entry<Integer, String> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
System.out.println("\nキーを個別に走査 Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\n値を個別に走査 Value");
for (String val : map.values()) {
System.out.println(val);
}
}
}

View File

@@ -0,0 +1,148 @@
/**
* File: hash_map_chaining.java
* Created Time: 2023-06-13
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.ArrayList;
import java.util.List;
/* チェイン法ハッシュテーブル */
class HashMapChaining {
int size; // キー値ペアの数
int capacity; // ハッシュテーブルの容量
double loadThres; // 拡張をトリガーする負荷率の閾値
int extendRatio; // 拡張倍率
List<List<Pair>> buckets; // バケット配列
/* コンストラクタ */
public HashMapChaining() {
size = 0;
capacity = 4;
loadThres = 2.0 / 3.0;
extendRatio = 2;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
}
/* ハッシュ関数 */
int hashFunc(int key) {
return key % capacity;
}
/* 負荷率 */
double loadFactor() {
return (double) size / capacity;
}
/* クエリ操作 */
String get(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// バケットを走査、キーが見つかった場合対応するvalを返す
for (Pair pair : bucket) {
if (pair.key == key) {
return pair.val;
}
}
// キーが見つからない場合、nullを返す
return null;
}
/* 追加操作 */
void put(int key, String val) {
// 負荷率が閾値を超えた場合、拡張を実行
if (loadFactor() > loadThres) {
extend();
}
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// バケットを走査、指定したキーに遭遇した場合、対応するvalを更新して戻る
for (Pair pair : bucket) {
if (pair.key == key) {
pair.val = val;
return;
}
}
// キーが見つからない場合、キー値ペアを末尾に追加
Pair pair = new Pair(key, val);
bucket.add(pair);
size++;
}
/* 削除操作 */
void remove(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// バケットを走査、その中からキー値ペアを削除
for (Pair pair : bucket) {
if (pair.key == key) {
bucket.remove(pair);
size--;
break;
}
}
}
/* ハッシュテーブルを拡張 */
void extend() {
// 元のハッシュテーブルを一時的に保存
List<List<Pair>> bucketsTmp = buckets;
// 拡張された新しいハッシュテーブルを初期化
capacity *= extendRatio;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
size = 0;
// 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
for (List<Pair> bucket : bucketsTmp) {
for (Pair pair : bucket) {
put(pair.key, pair.val);
}
}
}
/* ハッシュテーブルを印刷 */
void print() {
for (List<Pair> bucket : buckets) {
List<String> res = new ArrayList<>();
for (Pair pair : bucket) {
res.add(pair.key + " -> " + pair.val);
}
System.out.println(res);
}
}
}
public class hash_map_chaining {
public static void main(String[] args) {
/* ハッシュテーブルを初期化 */
HashMapChaining map = new HashMapChaining();
/* 追加操作 */
// ハッシュテーブルにキー値ペア (key, value) を追加
map.put(12836, "Ha");
map.put(15937, "Luo");
map.put(16750, "Suan");
map.put(13276, "Fa");
map.put(10583, "Ya");
System.out.println("\n追加後のハッシュテーブル\nKey -> Value");
map.print();
/* クエリ操作 */
// ハッシュテーブルにキーを入力して値を取得
String name = map.get(13276);
System.out.println("\n学生ID 13276を入力、名前 " + name + " を見つけました");
/* 削除操作 */
// ハッシュテーブルからキー値ペア (key, value) を削除
map.remove(12836);
System.out.println("\n12836を削除後のハッシュテーブル\nKey -> Value");
map.print();
}
}

View File

@@ -0,0 +1,158 @@
/**
* File: hash_map_open_addressing.java
* Created Time: 2023-06-13
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
/* オープンアドレス法ハッシュテーブル */
class HashMapOpenAddressing {
private int size; // キー値ペアの数
private int capacity = 4; // ハッシュテーブルの容量
private final double loadThres = 2.0 / 3.0; // 拡張をトリガーする負荷率の閾値
private final int extendRatio = 2; // 拡張倍率
private Pair[] buckets; // バケット配列
private final Pair TOMBSTONE = new Pair(-1, "-1"); // 削除マーク
/* コンストラクタ */
public HashMapOpenAddressing() {
size = 0;
buckets = new Pair[capacity];
}
/* ハッシュ関数 */
private int hashFunc(int key) {
return key % capacity;
}
/* 負荷率 */
private double loadFactor() {
return (double) size / capacity;
}
/* keyに対応するバケットインデックスを検索 */
private int findBucket(int key) {
int index = hashFunc(key);
int firstTombstone = -1;
// 線形探査、空のバケットに遭遇したら終了
while (buckets[index] != null) {
// keyに遭遇した場合、対応するバケットインデックスを返す
if (buckets[index].key == key) {
// 以前に削除マークに遭遇していた場合、キー値ペアをそのインデックスに移動
if (firstTombstone != -1) {
buckets[firstTombstone] = buckets[index];
buckets[index] = TOMBSTONE;
return firstTombstone; // 移動後のバケットインデックスを返す
}
return index; // バケットインデックスを返す
}
// 最初に遭遇した削除マークを記録
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
firstTombstone = index;
}
// バケットインデックスを計算、末尾を超えた場合は先頭に戻る
index = (index + 1) % capacity;
}
// keyが存在しない場合、挿入ポイントのインデックスを返す
return firstTombstone == -1 ? index : firstTombstone;
}
/* クエリ操作 */
public String get(int key) {
// keyに対応するバケットインデックスを検索
int index = findBucket(key);
// キー値ペアが見つかった場合、対応するvalを返す
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index].val;
}
// キー値ペアが存在しない場合、nullを返す
return null;
}
/* 追加操作 */
public void put(int key, String val) {
// 負荷率が閾値を超えた場合、拡張を実行
if (loadFactor() > loadThres) {
extend();
}
// keyに対応するバケットインデックスを検索
int index = findBucket(key);
// キー値ペアが見つかった場合、valを上書きして戻る
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index].val = val;
return;
}
// キー値ペアが存在しない場合、キー値ペアを追加
buckets[index] = new Pair(key, val);
size++;
}
/* 削除操作 */
public void remove(int key) {
// keyに対応するバケットインデックスを検索
int index = findBucket(key);
// キー値ペアが見つかった場合、削除マークで覆う
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE;
size--;
}
}
/* ハッシュテーブルを拡張 */
private void extend() {
// 元のハッシュテーブルを一時的に保存
Pair[] bucketsTmp = buckets;
// 拡張された新しいハッシュテーブルを初期化
capacity *= extendRatio;
buckets = new Pair[capacity];
size = 0;
// 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
for (Pair pair : bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.val);
}
}
}
/* ハッシュテーブルを印刷 */
public void print() {
for (Pair pair : buckets) {
if (pair == null) {
System.out.println("null");
} else if (pair == TOMBSTONE) {
System.out.println("TOMBSTONE");
} else {
System.out.println(pair.key + " -> " + pair.val);
}
}
}
}
public class hash_map_open_addressing {
public static void main(String[] args) {
// ハッシュテーブルを初期化
HashMapOpenAddressing hashmap = new HashMapOpenAddressing();
// 追加操作
// ハッシュテーブルにキー値ペア (key, val) を追加
hashmap.put(12836, "Ha");
hashmap.put(15937, "Luo");
hashmap.put(16750, "Suan");
hashmap.put(13276, "Fa");
hashmap.put(10583, "Ya");
System.out.println("\n追加後のハッシュテーブル\nKey -> Value");
hashmap.print();
// クエリ操作
// ハッシュテーブルにキーを入力して値valを取得
String name = hashmap.get(13276);
System.out.println("\n学生ID 13276を入力、名前 " + name + " を見つけました");
// 削除操作
// ハッシュテーブルからキー値ペア (key, val) を削除
hashmap.remove(16750);
System.out.println("\n16750を削除後のハッシュテーブル\nKey -> Value");
hashmap.print();
}
}

View File

@@ -0,0 +1,65 @@
/**
* File: simple_hash.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
public class simple_hash {
/* 加算ハッシュ */
static int addHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = (hash + (int) c) % MODULUS;
}
return (int) hash;
}
/* 乗算ハッシュ */
static int mulHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = (31 * hash + (int) c) % MODULUS;
}
return (int) hash;
}
/* XORハッシュ */
static int xorHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash ^= (int) c;
}
return hash & MODULUS;
}
/* 回転ハッシュ */
static int rotHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;
}
return (int) hash;
}
public static void main(String[] args) {
String key = "Hello algorithm";
int hash = addHash(key);
System.out.println("加算ハッシュ値は " + hash + " です");
hash = mulHash(key);
System.out.println("乗算ハッシュ値は " + hash + " です");
hash = xorHash(key);
System.out.println("XORハッシュ値は " + hash + " です");
hash = rotHash(key);
System.out.println("回転ハッシュ値は " + hash + " です");
}
}