mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 11:29:51 +08:00
* 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>
49 lines
1.9 KiB
Java
49 lines
1.9 KiB
Java
/**
|
|
* File: binary_search_edge.java
|
|
* Created Time: 2023-08-04
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_searching;
|
|
|
|
public class binary_search_edge {
|
|
/* 最も左の target を二分探索 */
|
|
static 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 を二分探索 */
|
|
static 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;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
// 重複要素を含む配列
|
|
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
|
|
System.out.println("\n配列 nums = " + java.util.Arrays.toString(nums));
|
|
|
|
// 左右の境界を二分探索
|
|
for (int target : new int[] { 6, 7 }) {
|
|
int index = binarySearchLeftEdge(nums, target);
|
|
System.out.println("要素 " + target + " の最も左のインデックスは " + index);
|
|
index = binarySearchRightEdge(nums, target);
|
|
System.out.println("要素 " + target + " の最も右のインデックスは " + index);
|
|
}
|
|
}
|
|
} |