mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-15 08:26:06 +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,59 @@
|
||||
/**
|
||||
* File: binary_search.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class binary_search {
|
||||
/* 二分探索(両閉区間) */
|
||||
int BinarySearch(int[] nums, int target) {
|
||||
// 両閉区間 [0, n-1] を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素を指す
|
||||
int i = 0, j = nums.Length - 1;
|
||||
// ループし、探索区間が空になったら終了する(i > j で空)
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // 中点インデックス m を計算
|
||||
if (nums[m] < target) // この場合、target は区間 [m+1, j] にある
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // この場合、target は区間 [i, m-1] にある
|
||||
j = m - 1;
|
||||
else // 目標要素が見つかったらそのインデックスを返す
|
||||
return m;
|
||||
}
|
||||
// 目標要素が見つからなければ -1 を返す
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 二分探索(左閉右開区間) */
|
||||
int BinarySearchLCRO(int[] nums, int target) {
|
||||
// 左閉右開区間 [0, n) を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素+1を指す
|
||||
int i = 0, j = nums.Length;
|
||||
// ループし、探索区間が空になったら終了する(i = j で空)
|
||||
while (i < j) {
|
||||
int m = i + (j - i) / 2; // 中点インデックス m を計算
|
||||
if (nums[m] < target) // この場合、target は区間 [m+1, j) にある
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // この場合、target は区間 [i, m) にある
|
||||
j = m;
|
||||
else // 目標要素が見つかったらそのインデックスを返す
|
||||
return m;
|
||||
}
|
||||
// 目標要素が見つからなければ -1 を返す
|
||||
return -1;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 6;
|
||||
int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
|
||||
/* 二分探索(両閉区間) */
|
||||
int index = BinarySearch(nums, target);
|
||||
Console.WriteLine("対象要素 6 のインデックス = " + index);
|
||||
|
||||
/* 二分探索(左閉右開区間) */
|
||||
index = BinarySearchLCRO(nums, target);
|
||||
Console.WriteLine("対象要素 6 のインデックス = " + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: binary_search_edge.cs
|
||||
* Created Time: 2023-08-06
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class binary_search_edge {
|
||||
/* 最も左の target を二分探索 */
|
||||
int BinarySearchLeftEdge(int[] nums, int target) {
|
||||
// target の挿入位置を探すのと等価
|
||||
int i = binary_search_insertion.BinarySearchInsertion(nums, target);
|
||||
// target が見つからなければ、-1 を返す
|
||||
if (i == nums.Length || nums[i] != target) {
|
||||
return -1;
|
||||
}
|
||||
// target が見つかったら、インデックス i を返す
|
||||
return i;
|
||||
}
|
||||
|
||||
/* 最も右の target を二分探索 */
|
||||
int BinarySearchRightEdge(int[] nums, int target) {
|
||||
// 最左の target + 1 を探す問題に変換する
|
||||
int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);
|
||||
// j は最も右の target を指し、i は target より大きい最初の要素を指す
|
||||
int j = i - 1;
|
||||
// target が見つからなければ、-1 を返す
|
||||
if (j == -1 || nums[j] != target) {
|
||||
return -1;
|
||||
}
|
||||
// target が見つかったら、インデックス j を返す
|
||||
return j;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 重複要素を含む配列
|
||||
int[] nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
Console.WriteLine("\n配列 nums = " + nums.PrintList());
|
||||
|
||||
// 二分探索で左端と右端を探す
|
||||
foreach (int target in new int[] { 6, 7 }) {
|
||||
int index = BinarySearchLeftEdge(nums, target);
|
||||
Console.WriteLine("一番左の要素 " + target + " のインデックスは " + index);
|
||||
index = BinarySearchRightEdge(nums, target);
|
||||
Console.WriteLine("一番右の要素 " + target + " のインデックスは " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* File: binary_search_insertion.cs
|
||||
* Created Time: 2023-08-06
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class binary_search_insertion {
|
||||
/* 二分探索で挿入位置を探す(重複要素なし) */
|
||||
public static int BinarySearchInsertionSimple(int[] nums, int target) {
|
||||
int i = 0, j = nums.Length - 1; // 両閉区間 [0, n-1] を初期化
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // 中点インデックス m を計算
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // target は区間 [m+1, j] にある
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // target は区間 [i, m-1] にある
|
||||
} else {
|
||||
return m; // target が見つかったら、挿入位置 m を返す
|
||||
}
|
||||
}
|
||||
// target が見つからなければ、挿入位置 i を返す
|
||||
return i;
|
||||
}
|
||||
|
||||
/* 二分探索で挿入位置を探す(重複要素あり) */
|
||||
public static int BinarySearchInsertion(int[] nums, int target) {
|
||||
int i = 0, j = nums.Length - 1; // 両閉区間 [0, n-1] を初期化
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // 中点インデックス m を計算
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // target は区間 [m+1, j] にある
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // target は区間 [i, m-1] にある
|
||||
} else {
|
||||
j = m - 1; // target より小さい最初の要素は区間 [i, m-1] にある
|
||||
}
|
||||
}
|
||||
// 挿入位置 i を返す
|
||||
return i;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 重複要素のない配列
|
||||
int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
Console.WriteLine("\n配列 nums = " + nums.PrintList());
|
||||
// 二分探索で挿入位置を探す
|
||||
foreach (int target in new int[] { 6, 9 }) {
|
||||
int index = BinarySearchInsertionSimple(nums, target);
|
||||
Console.WriteLine("要素 " + target + " の挿入位置のインデックスは " + index);
|
||||
}
|
||||
|
||||
// 重複要素を含む配列
|
||||
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
Console.WriteLine("\n配列 nums = " + nums.PrintList());
|
||||
// 二分探索で挿入位置を探す
|
||||
foreach (int target in new int[] { 2, 6, 20 }) {
|
||||
int index = BinarySearchInsertion(nums, target);
|
||||
Console.WriteLine("要素 " + target + " の挿入位置のインデックスは " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: hashing_search.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class hashing_search {
|
||||
/* ハッシュ探索(配列) */
|
||||
int HashingSearchArray(Dictionary<int, int> map, int target) {
|
||||
// ハッシュテーブルの key: 目標要素、value: インデックス
|
||||
// ハッシュテーブルにこの key がなければ -1 を返す
|
||||
return map.GetValueOrDefault(target, -1);
|
||||
}
|
||||
|
||||
/* ハッシュ探索(連結リスト) */
|
||||
ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
|
||||
|
||||
// ハッシュテーブルの key: 目標ノード値、value: ノードオブジェクト
|
||||
// ハッシュテーブルにこの key がなければ null を返す
|
||||
return map.GetValueOrDefault(target);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 3;
|
||||
|
||||
/* ハッシュ探索(配列) */
|
||||
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
// ハッシュテーブルを初期化
|
||||
Dictionary<int, int> map = [];
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
map[nums[i]] = i; // key: 要素、value: インデックス
|
||||
}
|
||||
int index = HashingSearchArray(map, target);
|
||||
Console.WriteLine("対象要素 3 のインデックス = " + index);
|
||||
|
||||
/* ハッシュ探索(連結リスト) */
|
||||
ListNode? head = ListNode.ArrToLinkedList(nums);
|
||||
// ハッシュテーブルを初期化
|
||||
Dictionary<int, ListNode> map1 = [];
|
||||
while (head != null) {
|
||||
map1[head.val] = head; // key: ノード値、value: ノード
|
||||
head = head.next;
|
||||
}
|
||||
ListNode? node = HashingSearchLinkedList(map1, target);
|
||||
Console.WriteLine("目標ノード値 3 に対応するノードオブジェクトは " + node);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: linear_search.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class linear_search {
|
||||
/* 線形探索(配列) */
|
||||
int LinearSearchArray(int[] nums, int target) {
|
||||
// 配列を走査
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
// 目標要素が見つかったらそのインデックスを返す
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// 目標要素が見つからなければ -1 を返す
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 線形探索(連結リスト) */
|
||||
ListNode? LinearSearchLinkedList(ListNode? head, int target) {
|
||||
// 連結リストを走査
|
||||
while (head != null) {
|
||||
// 対象ノードが見つかったら、それを返す
|
||||
if (head.val == target)
|
||||
return head;
|
||||
head = head.next;
|
||||
}
|
||||
// 対象ノードが見つからない場合は null を返す
|
||||
return null;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 3;
|
||||
|
||||
/* 配列で線形探索を行う */
|
||||
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
|
||||
int index = LinearSearchArray(nums, target);
|
||||
Console.WriteLine("対象要素 3 のインデックス = " + index);
|
||||
|
||||
/* 連結リストで線形探索を行う */
|
||||
ListNode? head = ListNode.ArrToLinkedList(nums);
|
||||
ListNode? node = LinearSearchLinkedList(head, target);
|
||||
Console.WriteLine("目標ノード値 3 に対応するノードオブジェクトは " + node);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: two_sum.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_searching;
|
||||
|
||||
public class two_sum {
|
||||
/* 方法 1:総当たり列挙 */
|
||||
int[] TwoSumBruteForce(int[] nums, int target) {
|
||||
int size = nums.Length;
|
||||
// 2重ループのため、時間計算量は O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return [i, j];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/* 方法 2:補助ハッシュテーブル */
|
||||
int[] TwoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.Length;
|
||||
// 補助ハッシュテーブルを使用し、空間計算量は O(n)
|
||||
Dictionary<int, int> dic = [];
|
||||
// 単一ループで、時間計算量は O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.ContainsKey(target - nums[i])) {
|
||||
return [dic[target - nums[i]], i];
|
||||
}
|
||||
dic.Add(nums[i], i);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// ======= Test Case =======
|
||||
int[] nums = [2, 7, 11, 15];
|
||||
int target = 13;
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法 1
|
||||
int[] res = TwoSumBruteForce(nums, target);
|
||||
Console.WriteLine("方法1 res = " + string.Join(",", res));
|
||||
// 方法 2
|
||||
res = TwoSumHashTable(nums, target);
|
||||
Console.WriteLine("方法2 res = " + string.Join(",", res));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user