mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-18 01:37:17 +08: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:
3
ja/codes/cpp/chapter_divide_and_conquer/CMakeLists.txt
Normal file
3
ja/codes/cpp/chapter_divide_and_conquer/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
add_executable(binary_search_recur binary_search_recur.cpp)
|
||||
add_executable(build_tree build_tree.cpp)
|
||||
add_executable(hanota hanota.cpp)
|
||||
@@ -8,20 +8,20 @@
|
||||
|
||||
/* 二分探索:問題 f(i, j) */
|
||||
int dfs(vector<int> &nums, int target, int i, int j) {
|
||||
// 区間が空の場合、対象要素が存在しないことを示すため、-1 を返す
|
||||
// 区間が空なら対象要素は存在しないので -1 を返す
|
||||
if (i > j) {
|
||||
return -1;
|
||||
}
|
||||
// 中点インデックス m を計算
|
||||
int m = i + (j - i) / 2;
|
||||
int m = (i + j) / 2;
|
||||
if (nums[m] < target) {
|
||||
// 再帰的な部分問題 f(m+1, j)
|
||||
// 部分問題 f(m+1, j) を再帰的に解く
|
||||
return dfs(nums, target, m + 1, j);
|
||||
} else if (nums[m] > target) {
|
||||
// 再帰的な部分問題 f(i, m-1)
|
||||
// 部分問題 f(i, m-1) を再帰的に解く
|
||||
return dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// 対象要素が見つかったため、そのインデックスを返す
|
||||
// 目標要素が見つかったらそのインデックスを返す
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -33,14 +33,14 @@ int binarySearch(vector<int> &nums, int target) {
|
||||
return dfs(nums, target, 0, n - 1);
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 6;
|
||||
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
|
||||
|
||||
// 二分探索(両端閉区間)
|
||||
// 二分探索(両閉区間)
|
||||
int index = binarySearch(nums, target);
|
||||
cout << "対象要素 6 のインデックス =" << index << endl;
|
||||
cout << "対象要素 6 のインデックス = " << index << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -6,26 +6,26 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 二分木の構築:分割統治 */
|
||||
/* 二分木を構築:分割統治 */
|
||||
TreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {
|
||||
// 部分木の区間が空の場合に終了
|
||||
// 部分木区間が空なら終了する
|
||||
if (r - l < 0)
|
||||
return NULL;
|
||||
// ルートノードを初期化
|
||||
// ルートノードを初期化する
|
||||
TreeNode *root = new TreeNode(preorder[i]);
|
||||
// m を問い合わせて左右の部分木を分割
|
||||
// m を求めて左右部分木を分割する
|
||||
int m = inorderMap[preorder[i]];
|
||||
// 部分問題:左の部分木を構築
|
||||
// 部分問題:左部分木を構築する
|
||||
root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);
|
||||
// 部分問題:右の部分木を構築
|
||||
// 部分問題:右部分木を構築する
|
||||
root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
|
||||
// ルートノードを返す
|
||||
// 根ノードを返す
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 二分木の構築 */
|
||||
/* 二分木を構築 */
|
||||
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
|
||||
// ハッシュテーブルを初期化し、中間順序の要素からインデックスへのマッピングを格納
|
||||
// inorder の要素からインデックスへの対応を格納するハッシュテーブルを初期化する
|
||||
unordered_map<int, int> inorderMap;
|
||||
for (int i = 0; i < inorder.size(); i++) {
|
||||
inorderMap[inorder[i]] = i;
|
||||
@@ -34,18 +34,18 @@ TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> preorder = {3, 9, 2, 1, 7};
|
||||
vector<int> inorder = {9, 3, 1, 2, 7};
|
||||
cout << "前順走査 = ";
|
||||
printVector(preorder);
|
||||
cout << "中間順序走査 = ";
|
||||
cout << "中順走査 = ";
|
||||
printVector(inorder);
|
||||
|
||||
TreeNode *root = buildTree(preorder, inorder);
|
||||
cout << "構築された二分木:\n";
|
||||
cout << "構築した二分木:\n";
|
||||
printTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,45 +6,45 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 円盤を移動 */
|
||||
/* 円盤を 1 枚移動 */
|
||||
void move(vector<int> &src, vector<int> &tar) {
|
||||
// src の最上部から円盤を取り出す
|
||||
// src の上から円盤を1枚取り出す
|
||||
int pan = src.back();
|
||||
src.pop_back();
|
||||
// 円盤を tar の最上部に配置
|
||||
// 円盤を tar の上に置く
|
||||
tar.push_back(pan);
|
||||
}
|
||||
|
||||
/* ハノイの塔問題 f(i) を解く */
|
||||
/* ハノイの塔の問題 f(i) を解く */
|
||||
void dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {
|
||||
// src に円盤が1つだけ残っている場合、それを tar に移動
|
||||
// src に円盤が 1 枚だけ残っている場合は、そのまま tar へ移す
|
||||
if (i == 1) {
|
||||
move(src, tar);
|
||||
return;
|
||||
}
|
||||
// 部分問題 f(i-1):tar の助けを借りて、上位 i-1 個の円盤を src から buf に移動
|
||||
// 部分問題 f(i-1):src の上部 i-1 枚の円盤を tar を補助にして buf へ移す
|
||||
dfs(i - 1, src, tar, buf);
|
||||
// 部分問題 f(1):残りの1つの円盤を src から tar に移動
|
||||
// 部分問題 f(1):src に残る 1 枚の円盤を tar に移す
|
||||
move(src, tar);
|
||||
// 部分問題 f(i-1):src の助けを借りて、上位 i-1 個の円盤を buf から tar に移動
|
||||
// 部分問題 f(i-1):buf の上部 i-1 枚の円盤を src を補助にして tar へ移す
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* ハノイの塔問題を解く */
|
||||
/* ハノイの塔を解く */
|
||||
void solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {
|
||||
int n = A.size();
|
||||
// B の助けを借りて、上位 n 個の円盤を A から C に移動
|
||||
// A の上から n 枚の円盤を B を介して C へ移す
|
||||
dfs(n, A, B, C);
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// リストの末尾が柱の最上部
|
||||
// リスト末尾が柱の頂上
|
||||
vector<int> A = {5, 4, 3, 2, 1};
|
||||
vector<int> B = {};
|
||||
vector<int> C = {};
|
||||
|
||||
cout << "初期状態:\n";
|
||||
cout << "初期状態:\n";
|
||||
cout << "A =";
|
||||
printVector(A);
|
||||
cout << "B =";
|
||||
@@ -54,7 +54,7 @@ int main() {
|
||||
|
||||
solveHanota(A, B, C);
|
||||
|
||||
cout << "円盤移動後:\n";
|
||||
cout << "円盤の移動完了後:\n";
|
||||
cout << "A =";
|
||||
printVector(A);
|
||||
cout << "B =";
|
||||
@@ -63,4 +63,4 @@ int main() {
|
||||
printVector(C);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user