mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Add the dart implementation of 0704.
This commit is contained in:
@ -755,8 +755,56 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
**Dart:**
|
||||
|
||||
|
||||
|
||||
```dart
|
||||
(版本一)左闭右闭区间
|
||||
class Solution {
|
||||
int search(List<int> nums, int target) {
|
||||
int left = 0;
|
||||
int right = nums.length - 1;
|
||||
while (left <= right) {
|
||||
int middle = ((left + right)/2).truncate();
|
||||
switch (nums[middle].compareTo(target)) {
|
||||
case 1:
|
||||
right = middle - 1;
|
||||
continue;
|
||||
case -1:
|
||||
left = middle + 1;
|
||||
continue;
|
||||
default:
|
||||
return middle;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
(版本二)左闭右开区间
|
||||
class Solution {
|
||||
int search(List<int> nums, int target) {
|
||||
int left = 0;
|
||||
int right = nums.length;
|
||||
while (left < right) {
|
||||
int middle = left + ((right - left) >> 1);
|
||||
switch (nums[middle].compareTo(target)) {
|
||||
case 1:
|
||||
right = middle;
|
||||
continue;
|
||||
case -1:
|
||||
left = middle + 1;
|
||||
continue;
|
||||
default:
|
||||
return middle;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user