File tree Expand file tree Collapse file tree 1 file changed +23
-2
lines changed Expand file tree Collapse file tree 1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -553,9 +553,11 @@ public class BinarySearch {
553
553
int low= 0 ,high= A . lenght- 1 ;
554
554
while (low <= high){
555
555
int mid = (low + high)/ 2 ;
556
- if (A [mid] >= val){
556
+ if (A [mid] >= val) {
557
557
high = mid- 1 ;
558
- }else low = mid+ 1 ;
558
+ } else {
559
+ low = mid+ 1 ;
560
+ }
559
561
}
560
562
return low;
561
563
}
@@ -625,6 +627,25 @@ def bisect_right(A, x):
625
627
else: r = mid - 1
626
628
return l # 或者返回 r + 1
627
629
```
630
+ ##### Java
631
+
632
+ ``` java
633
+ import java.util.* ;
634
+ public class BinarySearch {
635
+ public int getPos (int [] A , int val ) {
636
+ int low= 0 ,high= A . lenght- 1 ;
637
+ while (low <= high){
638
+ int mid = (low + high)/ 2 ;
639
+ if (A [mid] <= val) {
640
+ low = mid + 1 ;
641
+ } else {
642
+ high = mid - 1 ;
643
+ }
644
+ }
645
+ return low;
646
+ }
647
+ }
648
+ ```
628
649
629
650
其他语言暂时空缺,欢迎
630
651
[ PR] ( https://github.com/azl397985856/leetcode-cheat/issues/4 )
You can’t perform that action at this time.
0 commit comments