Skip to content

Commit 3ba3288

Browse files
committed
Add the dart implementation of 0704.
1 parent 5974c82 commit 3ba3288

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
@@ -755,8 +755,56 @@ object Solution {
755755
}
756756
}
757757
```
758+
**Dart:**
758759

759760

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

0 commit comments

Comments
 (0)