mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 14:10:00 +08:00
1. Add build script for Java.
2. Add height limitation for code blocks in extra.css. 3. Fix "节点" to "结点".
This commit is contained in:
@@ -54,23 +54,7 @@ $$
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_search.java"
|
||||
/* 二分查找(双闭区间) */
|
||||
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) / 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;
|
||||
}
|
||||
[class]{binary_search}-[func]{binarySearch}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -235,23 +219,7 @@ $$
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_search.java"
|
||||
/* 二分查找(左闭右开) */
|
||||
int binarySearch1(int[] nums, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
int m = (i + j) / 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;
|
||||
}
|
||||
[class]{binary_search}-[func]{binarySearch1}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
@@ -19,12 +19,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="hashing_search.java"
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearchArray(Map<Integer, Integer> map, int target) {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
return map.getOrDefault(target, -1);
|
||||
}
|
||||
[class]{hashing_search}-[func]{hashingSearchArray}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -125,12 +120,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="hashing_search.java"
|
||||
/* 哈希查找(链表) */
|
||||
ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 null
|
||||
return map.getOrDefault(target, null);
|
||||
}
|
||||
[class]{hashing_search}-[func]{hashingSearchLinkedList}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
@@ -15,17 +15,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="linear_search.java"
|
||||
/* 线性查找(数组) */
|
||||
int linearSearchArray(int[] nums, int target) {
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 找到目标元素,返回其索引
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
[class]{hashing_search}-[func]{linearSearchArray}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -155,18 +145,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="linear_search.java"
|
||||
/* 线性查找(链表) */
|
||||
ListNode linearSearchLinkedList(ListNode head, int target) {
|
||||
// 遍历链表
|
||||
while (head != null) {
|
||||
// 找到目标结点,返回之
|
||||
if (head.val == target)
|
||||
return head;
|
||||
head = head.next;
|
||||
}
|
||||
// 未找到目标结点,返回 null
|
||||
return null;
|
||||
}
|
||||
[class]{hashing_search}-[func]{linearSearchLinkedList}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
Reference in New Issue
Block a user