build
This commit is contained in:
parent
971624c291
commit
f0826da7f6
@ -40,7 +40,8 @@ comments: true
|
|||||||
## 16.1.7. C# 环境
|
## 16.1.7. C# 环境
|
||||||
|
|
||||||
1. 下载并安装 [.Net 6.0](https://dotnet.microsoft.com/en-us/download) 。
|
1. 下载并安装 [.Net 6.0](https://dotnet.microsoft.com/en-us/download) 。
|
||||||
2. 在 VSCode 的插件市场中搜索 `c#` ,安装 c# 。
|
2. 在 VSCode 的插件市场中搜索 `C# Dev Kit` ,安装 C# Dev Kit ([配置教程](https://code.visualstudio.com/docs/csharp/get-started))。
|
||||||
|
3. 也可使用 Visual Studio([安装教程](https://learn.microsoft.com/zh-cn/visualstudio/install/install-visual-studio?view=vs-2022))。
|
||||||
|
|
||||||
## 16.1.8. Swift 环境
|
## 16.1.8. Swift 环境
|
||||||
|
|
||||||
|
@ -313,16 +313,16 @@ comments: true
|
|||||||
fn binary_search(nums: &[i32], target: i32) -> i32 {
|
fn binary_search(nums: &[i32], target: i32) -> i32 {
|
||||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut j = nums.len() - 1;
|
let mut j = nums.len() as i32 - 1;
|
||||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||||
while i <= j {
|
while i <= j {
|
||||||
let m = i + (j - i) / 2; // 计算中点索引 m
|
let m = i + (j - i) / 2; // 计算中点索引 m
|
||||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中
|
if nums[m as usize] < target { // 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m-1] 中
|
} else if nums[m as usize] > target { // 此情况说明 target 在区间 [i, m-1] 中
|
||||||
j = m - 1;
|
j = m - 1;
|
||||||
} else { // 找到目标元素,返回其索引
|
} else { // 找到目标元素,返回其索引
|
||||||
return m as i32;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 未找到目标元素,返回 -1
|
// 未找到目标元素,返回 -1
|
||||||
@ -604,16 +604,16 @@ comments: true
|
|||||||
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
|
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
|
||||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut j = nums.len();
|
let mut j = nums.len() as i32;
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while i < j {
|
while i < j {
|
||||||
let m = i + (j - i) / 2; // 计算中点索引 m
|
let m = i + (j - i) / 2; // 计算中点索引 m
|
||||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中
|
if nums[m as usize] < target { // 此情况说明 target 在区间 [m+1, j) 中
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中
|
} else if nums[m as usize] > target { // 此情况说明 target 在区间 [i, m) 中
|
||||||
j = m - 1;
|
j = m - 1;
|
||||||
} else { // 找到目标元素,返回其索引
|
} else { // 找到目标元素,返回其索引
|
||||||
return m as i32;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 未找到目标元素,返回 -1
|
// 未找到目标元素,返回 -1
|
||||||
|
@ -131,7 +131,17 @@ status: new
|
|||||||
=== "Dart"
|
=== "Dart"
|
||||||
|
|
||||||
```dart title="binary_search_edge.dart"
|
```dart title="binary_search_edge.dart"
|
||||||
[class]{}-[func]{binarySearchLeftEdge}
|
/* 二分查找最左一个 target */
|
||||||
|
int binarySearchLeftEdge(List<int> nums, int target) {
|
||||||
|
// 等价于查找 target 的插入点
|
||||||
|
int i = binarySearchInsertion(nums, target);
|
||||||
|
// 未找到 target ,返回 -1
|
||||||
|
if (i == nums.length || nums[i] != target) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// 找到 target ,返回索引 i
|
||||||
|
return i;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Rust"
|
=== "Rust"
|
||||||
@ -279,7 +289,19 @@ status: new
|
|||||||
=== "Dart"
|
=== "Dart"
|
||||||
|
|
||||||
```dart title="binary_search_edge.dart"
|
```dart title="binary_search_edge.dart"
|
||||||
[class]{}-[func]{binarySearchRightEdge}
|
/* 二分查找最右一个 target */
|
||||||
|
int binarySearchRightEdge(List<int> nums, int target) {
|
||||||
|
// 转化为查找最左一个 target + 1
|
||||||
|
int i = 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;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Rust"
|
=== "Rust"
|
||||||
|
@ -164,7 +164,22 @@ status: new
|
|||||||
=== "Dart"
|
=== "Dart"
|
||||||
|
|
||||||
```dart title="binary_search_insertion.dart"
|
```dart title="binary_search_insertion.dart"
|
||||||
[class]{}-[func]{binarySearchInsertionSimple}
|
/* 二分查找插入点(无重复元素) */
|
||||||
|
int binarySearchInsertionSimple(List<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;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Rust"
|
=== "Rust"
|
||||||
@ -362,7 +377,22 @@ status: new
|
|||||||
=== "Dart"
|
=== "Dart"
|
||||||
|
|
||||||
```dart title="binary_search_insertion.dart"
|
```dart title="binary_search_insertion.dart"
|
||||||
[class]{}-[func]{binarySearchInsertion}
|
/* 二分查找插入点(存在重复元素) */
|
||||||
|
int binarySearchInsertion(List<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;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Rust"
|
=== "Rust"
|
||||||
|
Loading…
Reference in New Issue
Block a user