mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-29 00:54:26 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -10,20 +10,20 @@ import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class array {
|
||||
/* 要素へのランダムアクセス */
|
||||
/* 要素へランダムアクセス */
|
||||
static int randomAccess(int[] nums) {
|
||||
// 区間 [0, nums.length) からランダムに数を選択
|
||||
// 区間 [0, nums.length) からランダムに 1 つの数を選ぶ
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// ランダム要素を取得して返す
|
||||
// ランダムな要素を取得して返す
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 配列長の拡張 */
|
||||
/* 配列長を拡張する */
|
||||
static int[] extend(int[] nums, int enlarge) {
|
||||
// 拡張された長さの配列を初期化
|
||||
// 拡張後の長さを持つ配列を初期化する
|
||||
int[] res = new int[nums.length + enlarge];
|
||||
// 元の配列のすべての要素を新しい配列にコピー
|
||||
// 元の配列の全要素を新しい配列にコピー
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
@@ -31,19 +31,19 @@ public class array {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* `index` に要素 num を挿入 */
|
||||
/* 配列の index 番目に要素 num を挿入 */
|
||||
static void insert(int[] nums, int num, int index) {
|
||||
// `index` より後のすべての要素を1つ後ろに移動
|
||||
// インデックス index 以降の全要素を 1 つ後ろへ移動する
|
||||
for (int i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index の要素に num を代入
|
||||
// index の要素に num を代入する
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* `index` の要素を削除 */
|
||||
/* index の要素を削除する */
|
||||
static void remove(int[] nums, int index) {
|
||||
// `index` より後のすべての要素を1つ前に移動
|
||||
// インデックス index より後ろの全要素を 1 つ前へ移動する
|
||||
for (int i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
@@ -52,17 +52,17 @@ public class array {
|
||||
/* 配列を走査 */
|
||||
static void traverse(int[] nums) {
|
||||
int count = 0;
|
||||
// インデックスによる配列の走査
|
||||
// インデックスで配列を走査
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 配列要素の走査
|
||||
// 配列要素を直接走査
|
||||
for (int num : nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列内で指定された要素を検索 */
|
||||
/* 配列内で指定要素を探す */
|
||||
static int find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target)
|
||||
@@ -71,7 +71,7 @@ public class array {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* 配列を初期化 */
|
||||
int[] arr = new int[5];
|
||||
@@ -81,25 +81,25 @@ public class array {
|
||||
|
||||
/* ランダムアクセス */
|
||||
int randomNum = randomAccess(nums);
|
||||
System.out.println("nums からランダム要素を取得 = " + randomNum);
|
||||
System.out.println("nums からランダムな要素を取得 " + randomNum);
|
||||
|
||||
/* 長さの拡張 */
|
||||
/* 長さを拡張 */
|
||||
nums = extend(nums, 3);
|
||||
System.out.println("配列の長さを8に拡張し、nums = " + Arrays.toString(nums));
|
||||
System.out.println("配列の長さを 8 に拡張し、nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 要素の挿入 */
|
||||
/* 要素を挿入する */
|
||||
insert(nums, 6, 3);
|
||||
System.out.println("インデックス3に数値6を挿入し、nums = " + Arrays.toString(nums));
|
||||
System.out.println("インデックス 3 に数値 6 を挿入し、nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 要素の削除 */
|
||||
/* 要素を削除 */
|
||||
remove(nums, 2);
|
||||
System.out.println("インデックス2の要素を削除し、nums = " + Arrays.toString(nums));
|
||||
System.out.println("インデックス 2 の要素を削除し、nums = " + Arrays.toString(nums));
|
||||
|
||||
/* 配列の走査 */
|
||||
/* 配列を走査 */
|
||||
traverse(nums);
|
||||
|
||||
/* 要素の検索 */
|
||||
/* 要素を探索する */
|
||||
int index = find(nums, 3);
|
||||
System.out.println("nums で要素3を見つけ、インデックス = " + index);
|
||||
System.out.println("nums 内で要素 3 を検索し、インデックス = " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ package chapter_array_and_linkedlist;
|
||||
import utils.*;
|
||||
|
||||
public class linked_list {
|
||||
/* 連結リストでノード n0 の後にノード P を挿入 */
|
||||
/* 連結リストでノード n0 の後ろにノード P を挿入する */
|
||||
static void insert(ListNode n0, ListNode P) {
|
||||
ListNode n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 連結リストでノード n0 の後の最初のノードを削除 */
|
||||
/* 連結リストでノード n0 の直後のノードを削除する */
|
||||
static void remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
@@ -26,7 +26,7 @@ public class linked_list {
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 連結リストの `index` のノードにアクセス */
|
||||
/* 連結リスト内で index 番目のノードにアクセス */
|
||||
static ListNode access(ListNode head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
@@ -36,7 +36,7 @@ public class linked_list {
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 連結リストで値 target を持つ最初のノードを検索 */
|
||||
/* 連結リストで値が target の最初のノードを探す */
|
||||
static int find(ListNode head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
@@ -48,39 +48,39 @@ public class linked_list {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* 連結リストの初期化 */
|
||||
/* 連結リストを初期化 */
|
||||
// 各ノードを初期化
|
||||
ListNode n0 = new ListNode(1);
|
||||
ListNode n1 = new ListNode(3);
|
||||
ListNode n2 = new ListNode(2);
|
||||
ListNode n3 = new ListNode(5);
|
||||
ListNode n4 = new ListNode(4);
|
||||
// ノード間の参照を構築
|
||||
// ノード間の参照を構築する
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
System.out.println("初期化された連結リストは");
|
||||
System.out.println("初期化した連結リストは");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* ノードの挿入 */
|
||||
/* ノードを挿入 */
|
||||
insert(n0, new ListNode(0));
|
||||
System.out.println("ノード挿入後の連結リストは");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* ノードの削除 */
|
||||
/* ノードを削除 */
|
||||
remove(n0);
|
||||
System.out.println("ノード削除後の連結リストは");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* ノードへのアクセス */
|
||||
/* ノードにアクセス */
|
||||
ListNode node = access(n0, 3);
|
||||
System.out.println("連結リストのインデックス3のノードの値 = " + node.val);
|
||||
System.out.println("連結リストのインデックス 3 にあるノードの値 = " + node.val);
|
||||
|
||||
/* ノードの検索 */
|
||||
/* ノードを探索 */
|
||||
int index = find(n0, 2);
|
||||
System.out.println("連結リストで値2を持つノードのインデックス = " + index);
|
||||
System.out.println("連結リスト内で値 2 のノードのインデックス = " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,23 +10,23 @@ import java.util.*;
|
||||
|
||||
public class list {
|
||||
public static void main(String[] args) {
|
||||
/* リストの初期化 */
|
||||
// 配列の要素型は Integer[]、int のラッパークラス
|
||||
/* リストを初期化 */
|
||||
// 配列の要素型は `int[]` のラッパークラスである `Integer[]` である点に注意
|
||||
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
||||
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
|
||||
System.out.println("リスト nums = " + nums);
|
||||
|
||||
/* 要素へのアクセス */
|
||||
/* 要素にアクセス */
|
||||
int num = nums.get(1);
|
||||
System.out.println("インデックス1の要素にアクセス、取得した num = " + num);
|
||||
System.out.println("インデックス 1 の要素にアクセスし、num = " + num);
|
||||
|
||||
/* 要素の更新 */
|
||||
/* 要素を更新 */
|
||||
nums.set(1, 0);
|
||||
System.out.println("インデックス1の要素を0に更新し、nums = " + nums);
|
||||
System.out.println("インデックス 1 の要素を 0 に更新し、nums = " + nums);
|
||||
|
||||
/* リストのクリア */
|
||||
/* リストを空にする */
|
||||
nums.clear();
|
||||
System.out.println("リストをクリアした後、nums = " + nums);
|
||||
System.out.println("リストを空にした後 nums = " + nums);
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
nums.add(1);
|
||||
@@ -34,33 +34,33 @@ public class list {
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
System.out.println("要素を追加した後、nums = " + nums);
|
||||
System.out.println("要素追加後 nums = " + nums);
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.add(3, 6);
|
||||
System.out.println("インデックス3に数値6を挿入し、nums = " + nums);
|
||||
System.out.println("インデックス 3 に数値 6 を挿入し、nums = " + nums);
|
||||
|
||||
/* 要素の削除 */
|
||||
/* 要素を削除 */
|
||||
nums.remove(3);
|
||||
System.out.println("インデックス3の要素を削除し、nums = " + nums);
|
||||
System.out.println("インデックス 3 の要素を削除し、nums = " + nums);
|
||||
|
||||
/* インデックスによるリストの走査 */
|
||||
/* インデックスでリストを走査 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums.get(i);
|
||||
}
|
||||
/* リスト要素の走査 */
|
||||
/* リスト要素を直接走査 */
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 2つのリストの連結 */
|
||||
/* 2 つのリストを連結する */
|
||||
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
|
||||
nums.addAll(nums1);
|
||||
System.out.println("リスト nums1 を nums に連結し、nums = " + nums);
|
||||
System.out.println("リスト nums1 を nums の後ろに連結し、nums = " + nums);
|
||||
|
||||
/* リストのソート */
|
||||
/* リストをソート */
|
||||
Collections.sort(nums);
|
||||
System.out.println("リストをソートした後、nums = " + nums);
|
||||
System.out.println("リストをソートした後 nums = " + nums);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,33 +12,33 @@ import java.util.*;
|
||||
class MyList {
|
||||
private int[] arr; // 配列(リスト要素を格納)
|
||||
private int capacity = 10; // リスト容量
|
||||
private int size = 0; // リスト長(現在の要素数)
|
||||
private int extendRatio = 2; // リストの各拡張倍率
|
||||
private int size = 0; // リストの長さ(現在の要素数)
|
||||
private int extendRatio = 2; // リスト拡張時の増加倍率
|
||||
|
||||
/* コンストラクタ */
|
||||
public MyList() {
|
||||
arr = new int[capacity];
|
||||
}
|
||||
|
||||
/* リスト長を取得(現在の要素数) */
|
||||
/* リストの長さを取得(現在の要素数) */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/* リスト容量を取得 */
|
||||
/* リスト容量を取得する */
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* 要素へのアクセス */
|
||||
/* 要素にアクセス */
|
||||
public int get(int index) {
|
||||
// インデックスが範囲外の場合、以下のように例外をスロー
|
||||
// インデックスが範囲外なら例外を送出する。以下同様
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("インデックスが範囲外です");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 要素の更新 */
|
||||
/* 要素を更新 */
|
||||
public void set(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("インデックスが範囲外です");
|
||||
@@ -47,7 +47,7 @@ class MyList {
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
public void add(int num) {
|
||||
// 要素数が容量を超える場合、拡張メカニズムを実行
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
arr[size] = num;
|
||||
@@ -59,10 +59,10 @@ class MyList {
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("インデックスが範囲外です");
|
||||
// 要素数が容量を超える場合、拡張メカニズムを実行
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
// `index` より後のすべての要素を1つ後ろに移動
|
||||
// index 以降の要素をすべて 1 つ後ろへずらす
|
||||
for (int j = size - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
@@ -71,12 +71,12 @@ class MyList {
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 要素の削除 */
|
||||
/* 要素を削除 */
|
||||
public int remove(int index) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("インデックスが範囲外です");
|
||||
int num = arr[index];
|
||||
// `index` より後のすべての要素を1つ前に移動
|
||||
// インデックス index より後の要素をすべて 1 つ前に移動する
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
@@ -86,18 +86,18 @@ class MyList {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* リストを拡張 */
|
||||
/* リストの拡張 */
|
||||
public void extendCapacity() {
|
||||
// 元の配列の長さを extendRatio 倍した新しい配列を作成し、元の配列を新しい配列にコピー
|
||||
// 元の配列の extendRatio 倍の長さを持つ新しい配列を作成し、元の配列をコピーする
|
||||
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
||||
// リスト容量を更新
|
||||
// リストの容量を更新
|
||||
capacity = arr.length;
|
||||
}
|
||||
|
||||
/* リストを配列に変換 */
|
||||
/* リストを配列に変換する */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
// 有効な長さ範囲内の要素のみを変換
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = get(i);
|
||||
@@ -107,9 +107,9 @@ class MyList {
|
||||
}
|
||||
|
||||
public class my_list {
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* リストの初期化 */
|
||||
/* リストを初期化 */
|
||||
MyList nums = new MyList();
|
||||
/* 末尾に要素を追加 */
|
||||
nums.add(1);
|
||||
@@ -118,30 +118,30 @@ public class my_list {
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
System.out.println("リスト nums = " + Arrays.toString(nums.toArray()) +
|
||||
", 容量 = " + nums.capacity() + ", 長さ = " + nums.size());
|
||||
" 、容量 = " + nums.capacity() + " 、長さ = " + nums.size());
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.insert(3, 6);
|
||||
System.out.println("インデックス3に数値6を挿入し、nums = " + Arrays.toString(nums.toArray()));
|
||||
System.out.println("インデックス 3 に数値 6 を挿入すると、nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 要素の削除 */
|
||||
/* 要素を削除 */
|
||||
nums.remove(3);
|
||||
System.out.println("インデックス3の要素を削除し、nums = " + Arrays.toString(nums.toArray()));
|
||||
System.out.println("インデックス 3 の要素を削除すると、nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 要素へのアクセス */
|
||||
/* 要素にアクセス */
|
||||
int num = nums.get(1);
|
||||
System.out.println("インデックス1の要素にアクセス、取得した num = " + num);
|
||||
System.out.println("インデックス 1 の要素にアクセスし、num = " + num);
|
||||
|
||||
/* 要素の更新 */
|
||||
/* 要素を更新 */
|
||||
nums.set(1, 0);
|
||||
System.out.println("インデックス1の要素を0に更新し、nums = " + Arrays.toString(nums.toArray()));
|
||||
System.out.println("インデックス 1 の要素を 0 に更新すると、nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* 拡張メカニズムのテスト */
|
||||
/* 拡張機構をテストする */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// i = 5 の時、リスト長がリスト容量を超え、この時点で拡張メカニズムが実行される
|
||||
// i = 5 のとき、リスト長が容量を超えるため、この時点で拡張機構が発動する
|
||||
nums.add(i);
|
||||
}
|
||||
System.out.println("拡張後、リスト nums = " + Arrays.toString(nums.toArray()) +
|
||||
", 容量 = " + nums.capacity() + ", 長さ = " + nums.size());
|
||||
System.out.println("拡張後のリスト nums = " + Arrays.toString(nums.toArray()) +
|
||||
" 、容量 = " + nums.capacity() + " 、長さ = " + nums.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user