Skip to content

Commit 6aad007

Browse files
Merge pull request youngyangyang04#2178 from yefeidd/feature/add-dart-implementation-of-0704
Add the dart implementation of 0704 - "二分查找"
2 parents 4f632c8 + 3ba3288 commit 6aad007

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

problems/0704.二分查找.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,56 @@ object Solution {
760760
}
761761
}
762762
```
763+
**Dart:**
763764

764765

766+
767+
```dart
768+
(版本一)左闭右闭区间
769+
class Solution {
770+
int search(List<int> nums, int target) {
771+
int left = 0;
772+
int right = nums.length - 1;
773+
while (left <= right) {
774+
int middle = ((left + right)/2).truncate();
775+
switch (nums[middle].compareTo(target)) {
776+
case 1:
777+
right = middle - 1;
778+
continue;
779+
case -1:
780+
left = middle + 1;
781+
continue;
782+
default:
783+
return middle;
784+
}
785+
}
786+
return -1;
787+
}
788+
}
789+
790+
(版本二)左闭右开区间
791+
class Solution {
792+
int search(List<int> nums, int target) {
793+
int left = 0;
794+
int right = nums.length;
795+
while (left < right) {
796+
int middle = left + ((right - left) >> 1);
797+
switch (nums[middle].compareTo(target)) {
798+
case 1:
799+
right = middle;
800+
continue;
801+
case -1:
802+
left = middle + 1;
803+
continue;
804+
default:
805+
return middle;
806+
}
807+
}
808+
return -1;
809+
}
810+
}
811+
```
812+
765813
<p align="center">
766814
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
767815
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
 (0)