mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
Polish some cotents.
This commit is contained in:
@@ -25,7 +25,7 @@ int binarySearch(int *nums, int len, int target) {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
int binarySearch1(int *nums, int len, int target) {
|
||||
int binarySearchLCRO(int *nums, int len, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = len;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
@@ -52,7 +52,7 @@ int main() {
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, 10, target);
|
||||
index = binarySearchLCRO(nums, 10, target);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -25,7 +25,7 @@ int binarySearch(vector<int> &nums, int target) {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
int binarySearch1(vector<int> &nums, int target) {
|
||||
int binarySearchLCRO(vector<int> &nums, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = nums.size();
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
@@ -52,7 +52,7 @@ int main() {
|
||||
cout << "目标元素 6 的索引 = " << index << endl;
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
index = binarySearchLCRO(nums, target);
|
||||
cout << "目标元素 6 的索引 = " << index << endl;
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class binary_search {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
static int binarySearch1(int[] nums, int target) {
|
||||
static int binarySearchLCRO(int[] nums, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = nums.Length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
@@ -53,7 +53,7 @@ public class binary_search {
|
||||
Console.WriteLine("目标元素 6 的索引 = " + index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
index = binarySearchLCRO(nums, target);
|
||||
Console.WriteLine("目标元素 6 的索引 = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func binarySearch(nums []int, target int) int {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
func binarySearch1(nums []int, target int) int {
|
||||
func binarySearchLCRO(nums []int, target int) int {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
i, j := 0, len(nums)
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
|
||||
@@ -26,7 +26,7 @@ public class binary_search {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
static int binarySearch1(int[] nums, int target) {
|
||||
static int binarySearchLCRO(int[] nums, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
@@ -52,7 +52,7 @@ public class binary_search {
|
||||
System.out.println("目标元素 6 的索引 = " + index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
index = binarySearchLCRO(nums, target);
|
||||
System.out.println("目标元素 6 的索引 = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ function binarySearch(nums, target) {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
function binarySearch1(nums, target) {
|
||||
function binarySearchLCRO(nums, target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let i = 0,
|
||||
j = nums.length;
|
||||
@@ -56,5 +56,5 @@ let index = binarySearch(nums, target);
|
||||
console.log('目标元素 6 的索引 = ' + index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
index = binarySearchLCRO(nums, target);
|
||||
console.log('目标元素 6 的索引 = ' + index);
|
||||
|
||||
@@ -20,7 +20,7 @@ def binary_search(nums: list[int], target: int) -> int:
|
||||
return -1 # 未找到目标元素,返回 -1
|
||||
|
||||
|
||||
def binary_search1(nums: list[int], target: int) -> int:
|
||||
def binary_search_lcro(nums: list[int], target: int) -> int:
|
||||
"""二分查找(左闭右开)"""
|
||||
# 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
i, j = 0, len(nums)
|
||||
@@ -46,5 +46,5 @@ if __name__ == "__main__":
|
||||
print("目标元素 6 的索引 = ", index)
|
||||
|
||||
# 二分查找(左闭右开)
|
||||
index: int = binary_search1(nums, target)
|
||||
index: int = binary_search_lcro(nums, target)
|
||||
print("目标元素 6 的索引 = ", index)
|
||||
|
||||
@@ -25,7 +25,7 @@ fn binary_search(nums: &[i32], target: i32) -> i32 {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
fn binary_search1(nums: &[i32], target: i32) -> i32 {
|
||||
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let mut i = 0;
|
||||
let mut j = nums.len();
|
||||
@@ -54,6 +54,6 @@ pub fn main() {
|
||||
println!("目标元素 6 的索引 = {index}");
|
||||
|
||||
// 二分查找(左闭右开)
|
||||
index = binary_search1(&nums, target);
|
||||
index = binary_search_lcro(&nums, target);
|
||||
println!("目标元素 6 的索引 = {index}");
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func binarySearch(nums: [Int], target: Int) -> Int {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
func binarySearch1(nums: [Int], target: Int) -> Int {
|
||||
func binarySearchLCRO(nums: [Int], target: Int) -> Int {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
var i = 0
|
||||
var j = nums.count
|
||||
@@ -56,7 +56,7 @@ enum BinarySearch {
|
||||
print("目标元素 6 的索引 = \(index)")
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums: nums, target: target)
|
||||
index = binarySearchLCRO(nums: nums, target: target)
|
||||
print("目标元素 6 的索引 = \(index)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ function binarySearch(nums: number[], target: number): number {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
function binarySearch1(nums: number[], target: number): number {
|
||||
function binarySearchLCRO(nums: number[], target: number): number {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let i = 0,
|
||||
j = nums.length;
|
||||
@@ -59,7 +59,7 @@ let index = binarySearch(nums, target);
|
||||
console.info('目标元素 6 的索引 = %d', index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
index = binarySearchLCRO(nums, target);
|
||||
console.info('目标元素 6 的索引 = %d', index);
|
||||
|
||||
export {};
|
||||
|
||||
@@ -26,7 +26,7 @@ fn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {
|
||||
}
|
||||
|
||||
// 二分查找(左闭右开)
|
||||
fn binarySearch1(comptime T: type, nums: std.ArrayList(T), target: T) T {
|
||||
fn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
var i: usize = 0;
|
||||
var j: usize = nums.items.len;
|
||||
@@ -57,7 +57,7 @@ pub fn main() !void {
|
||||
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
||||
|
||||
// 二分查找(左闭右开)
|
||||
index = binarySearch1(i32, nums, target);
|
||||
index = binarySearchLCRO(i32, nums, target);
|
||||
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
|
||||
Reference in New Issue
Block a user