Skip to content

Commit 138fe15

Browse files
Create 0704-binary-search.scala
1 parent 8de25ca commit 138fe15

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

scala/0704-binary-search.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object Solution {
2+
def search(nums: Array[Int], target: Int): Int = {
3+
var left = 0
4+
var right = nums.length - 1
5+
6+
while (left <= right) {
7+
val mid = left + (right - left) / 2
8+
9+
if (nums(mid) == target) {
10+
return mid
11+
} else if (nums(mid) < target) {
12+
left = mid + 1
13+
} else {
14+
right = mid - 1
15+
}
16+
}
17+
18+
return -1
19+
}
20+
}

0 commit comments

Comments
 (0)