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:
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* File: array.js
|
||||
* Created Time: 2022-11-27
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
/* 要素へランダムアクセス */
|
||||
function randomAccess(nums) {
|
||||
// 区間 [0, nums.length) からランダムに 1 つの数を選ぶ
|
||||
const random_index = Math.floor(Math.random() * nums.length);
|
||||
// ランダムな要素を取得して返す
|
||||
const random_num = nums[random_index];
|
||||
return random_num;
|
||||
}
|
||||
|
||||
/* 配列長を拡張する */
|
||||
// JavaScript の Array は動的配列であり、直接拡張できます
|
||||
// 学習しやすいよう、本関数では Array を長さ不変の配列として扱います
|
||||
function extend(nums, enlarge) {
|
||||
// 拡張後の長さを持つ配列を初期化する
|
||||
const res = new Array(nums.length + enlarge).fill(0);
|
||||
// 元の配列の全要素を新しい配列にコピー
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 拡張後の新しい配列を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 配列の index 番目に要素 num を挿入 */
|
||||
function insert(nums, num, index) {
|
||||
// インデックス index 以降の全要素を 1 つ後ろへ移動する
|
||||
for (let i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index の要素に num を代入する
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* index の要素を削除する */
|
||||
function remove(nums, index) {
|
||||
// インデックス index より後ろの全要素を 1 つ前へ移動する
|
||||
for (let i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列を走査 */
|
||||
function traverse(nums) {
|
||||
let count = 0;
|
||||
// インデックスで配列を走査
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 配列要素を直接走査
|
||||
for (const num of nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列内で指定要素を探す */
|
||||
function find(nums, target) {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (nums[i] === target) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 配列を初期化 */
|
||||
const arr = new Array(5).fill(0);
|
||||
console.log('配列 arr =', arr);
|
||||
let nums = [1, 3, 2, 5, 4];
|
||||
console.log('配列 nums =', nums);
|
||||
|
||||
/* ランダムアクセス */
|
||||
let random_num = randomAccess(nums);
|
||||
console.log('nums からランダム要素を取得', random_num);
|
||||
|
||||
/* 長さを拡張 */
|
||||
nums = extend(nums, 3);
|
||||
console.log('配列の長さを 8 に拡張し,nums =', nums);
|
||||
|
||||
/* 要素を挿入する */
|
||||
insert(nums, 6, 3);
|
||||
console.log('インデックス 3 に数字 6 を挿入し,nums =', nums);
|
||||
|
||||
/* 要素を削除 */
|
||||
remove(nums, 2);
|
||||
console.log('インデックス 2 の要素を削除し,nums =', nums);
|
||||
|
||||
/* 配列を走査 */
|
||||
traverse(nums);
|
||||
|
||||
/* 要素を探索する */
|
||||
let index = find(nums, 3);
|
||||
console.log('nums 内で要素 3 を検索し,インデックス =', index);
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* File: linked_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: IsChristina (christinaxia77@foxmail.com), Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { printLinkedList } = require('../modules/PrintUtil');
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 連結リストでノード n0 の後ろにノード P を挿入する */
|
||||
function insert(n0, P) {
|
||||
const n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 連結リストでノード n0 の直後のノードを削除する */
|
||||
function remove(n0) {
|
||||
if (!n0.next) return;
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 連結リスト内で index 番目のノードにアクセス */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
if (!head) {
|
||||
return null;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 連結リストで値が target の最初のノードを探す */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 連結リストを初期化 */
|
||||
// 各ノードを初期化
|
||||
const n0 = new ListNode(1);
|
||||
const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// ノード間の参照を構築する
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
console.log('初期化された連結リストは');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードを挿入 */
|
||||
insert(n0, new ListNode(0));
|
||||
console.log('ノード挿入後の連結リストは');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードを削除 */
|
||||
remove(n0);
|
||||
console.log('ノード削除後の連結リストは');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードにアクセス */
|
||||
const node = access(n0, 3);
|
||||
console.log('連結リストのインデックス 3 のノードの値 = ' + node.val);
|
||||
|
||||
/* ノードを探索 */
|
||||
const index = find(n0, 2);
|
||||
console.log('連結リスト内で値が 2 のノードのインデックス = ' + index);
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* File: list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* リストを初期化 */
|
||||
const nums = [1, 3, 2, 5, 4];
|
||||
console.log(`リスト nums = ${nums}`);
|
||||
|
||||
/* 要素にアクセス */
|
||||
const num = nums[1];
|
||||
console.log(`インデックス 1 の要素にアクセスし,num = ${num}`);
|
||||
|
||||
/* 要素を更新 */
|
||||
nums[1] = 0;
|
||||
console.log(`インデックス 1 の要素を 0 に更新し,nums = ${nums}`);
|
||||
|
||||
/* リストを空にする */
|
||||
nums.length = 0;
|
||||
console.log(`リストを空にした後,nums = ${nums}`);
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
nums.push(5);
|
||||
nums.push(4);
|
||||
console.log(`要素追加後,nums = ${nums}`);
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.splice(3, 0, 6);
|
||||
console.log(`インデックス 3 に数字 6 を挿入し,nums = ${nums}`);
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.splice(3, 1);
|
||||
console.log(`インデックス 3 の要素を削除し,nums = ${nums}`);
|
||||
|
||||
/* インデックスでリストを走査 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* リスト要素を直接走査 */
|
||||
count = 0;
|
||||
for (const x of nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 2 つのリストを連結する */
|
||||
const nums1 = [6, 8, 7, 10, 9];
|
||||
nums.push(...nums1);
|
||||
console.log(`リスト nums1 を nums の後ろに連結し,nums = ${nums}`);
|
||||
|
||||
/* リストをソート */
|
||||
nums.sort((a, b) => a - b);
|
||||
console.log(`リストをソート後,nums = ${nums}`);
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: my_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* リストクラス */
|
||||
class MyList {
|
||||
#arr = new Array(); // 配列(リスト要素を格納)
|
||||
#capacity = 10; // リスト容量
|
||||
#size = 0; // リストの長さ(現在の要素数)
|
||||
#extendRatio = 2; // リスト拡張時の増加倍率
|
||||
|
||||
/* コンストラクタ */
|
||||
constructor() {
|
||||
this.#arr = new Array(this.#capacity);
|
||||
}
|
||||
|
||||
/* リストの長さを取得(現在の要素数) */
|
||||
size() {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
/* リスト容量を取得する */
|
||||
capacity() {
|
||||
return this.#capacity;
|
||||
}
|
||||
|
||||
/* 要素にアクセス */
|
||||
get(index) {
|
||||
// インデックスが範囲外なら例外を送出する。以下同様
|
||||
if (index < 0 || index >= this.#size) throw new Error('インデックスが範囲外です');
|
||||
return this.#arr[index];
|
||||
}
|
||||
|
||||
/* 要素を更新 */
|
||||
set(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('インデックスが範囲外です');
|
||||
this.#arr[index] = num;
|
||||
}
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
add(num) {
|
||||
// 長さが容量に等しい場合は拡張が必要
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 新しい要素をリストの末尾に追加する
|
||||
this.#arr[this.#size] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('インデックスが範囲外です');
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// index 以降の要素をすべて 1 つ後ろへずらす
|
||||
for (let j = this.#size - 1; j >= index; j--) {
|
||||
this.#arr[j + 1] = this.#arr[j];
|
||||
}
|
||||
// 要素数を更新
|
||||
this.#arr[index] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 要素を削除 */
|
||||
remove(index) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('インデックスが範囲外です');
|
||||
let num = this.#arr[index];
|
||||
// インデックス index より後の要素をすべて 1 つ前に移動する
|
||||
for (let j = index; j < this.#size - 1; j++) {
|
||||
this.#arr[j] = this.#arr[j + 1];
|
||||
}
|
||||
// 要素数を更新
|
||||
this.#size--;
|
||||
// 削除された要素を返す
|
||||
return num;
|
||||
}
|
||||
|
||||
/* リストの拡張 */
|
||||
extendCapacity() {
|
||||
// 元の配列の extendRatio 倍の長さを持つ新しい配列を作成し、元の配列をコピーする
|
||||
this.#arr = this.#arr.concat(
|
||||
new Array(this.capacity() * (this.#extendRatio - 1))
|
||||
);
|
||||
// リストの容量を更新
|
||||
this.#capacity = this.#arr.length;
|
||||
}
|
||||
|
||||
/* リストを配列に変換する */
|
||||
toArray() {
|
||||
let size = this.size();
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
const arr = new Array(size);
|
||||
for (let i = 0; i < size; i++) {
|
||||
arr[i] = this.get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* リストを初期化 */
|
||||
const nums = new MyList();
|
||||
/* 末尾に要素を追加 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
console.log(
|
||||
`リスト nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,長さ = ${nums.size()}`
|
||||
);
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.insert(3, 6);
|
||||
console.log(`インデックス 3 に数字 6 を挿入し,nums = ${nums.toArray()}`);
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.remove(3);
|
||||
console.log(`インデックス 3 の要素を削除し,nums = ${nums.toArray()}`);
|
||||
|
||||
/* 要素にアクセス */
|
||||
const num = nums.get(1);
|
||||
console.log(`インデックス 1 の要素にアクセスし,num = ${num}`);
|
||||
|
||||
/* 要素を更新 */
|
||||
nums.set(1, 0);
|
||||
console.log(`インデックス 1 の要素を 0 に更新し,nums = ${nums.toArray()}`);
|
||||
|
||||
/* 拡張機構をテストする */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
// i = 5 のとき、リスト長が容量を超えるため、この時点で拡張機構が発動する
|
||||
nums.add(i);
|
||||
}
|
||||
console.log(
|
||||
`拡張後のリスト nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,長さ = ${nums.size()}`
|
||||
);
|
||||
Reference in New Issue
Block a user