mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 13:43:06 +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
66
ja/codes/cpp/chapter_searching/binary_search_insertion.cpp
Normal file
66
ja/codes/cpp/chapter_searching/binary_search_insertion.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: binary_search_insertion.cpp
|
||||
* Created Time: 2023-08-04
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 挿入ポイントの二分探索(重複要素なし) */
|
||||
int binarySearchInsertionSimple(vector<int> &nums, int target) {
|
||||
int i = 0, j = nums.size() - 1; // 両端閉区間[0, n-1]を初期化
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // 中点インデックスmを計算
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // ターゲットは区間[m+1, j]にある
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // ターゲットは区間[i, m-1]にある
|
||||
} else {
|
||||
return m; // ターゲットが見つかったため、挿入ポイントmを返す
|
||||
}
|
||||
}
|
||||
// ターゲットが見つからなかったため、挿入ポイントiを返す
|
||||
return i;
|
||||
}
|
||||
|
||||
/* 挿入ポイントの二分探索(重複要素あり) */
|
||||
int binarySearchInsertion(vector<int> &nums, int target) {
|
||||
int i = 0, j = nums.size() - 1; // 両端閉区間[0, n-1]を初期化
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // 中点インデックスmを計算
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // ターゲットは区間[m+1, j]にある
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // ターゲットは区間[i, m-1]にある
|
||||
} else {
|
||||
j = m - 1; // ターゲット未満の最初の要素は区間[i, m-1]にある
|
||||
}
|
||||
}
|
||||
// 挿入ポイントiを返す
|
||||
return i;
|
||||
}
|
||||
|
||||
/* ドライバコード */
|
||||
int main() {
|
||||
// 重複要素のない配列
|
||||
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
|
||||
cout << "\n配列 nums = ";
|
||||
printVector(nums);
|
||||
// 挿入ポイントの二分探索
|
||||
for (int target : {6, 9}) {
|
||||
int index = binarySearchInsertionSimple(nums, target);
|
||||
cout << "要素 " << target << " の挿入ポイントインデックスは " << index << " です" << endl;
|
||||
}
|
||||
|
||||
// 重複要素を含む配列
|
||||
nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
|
||||
cout << "\n配列 nums = ";
|
||||
printVector(nums);
|
||||
// 挿入ポイントの二分探索
|
||||
for (int target : {2, 6, 20}) {
|
||||
int index = binarySearchInsertion(nums, target);
|
||||
cout << "要素 " << target << " の挿入ポイントインデックスは " << index << " です" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user