Skip to content

Commit 930dd2c

Browse files
committed
Create 0035-search-insert-position.kt
1 parent b39259d commit 930dd2c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

kotlin/0035-search-insert-position.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
fun searchInsert(nums: IntArray, target: Int): Int {
3+
4+
var l = 0
5+
var r = nums.lastIndex
6+
7+
while(l <= r) {
8+
val mid = (l + r) / 2
9+
if (target == nums[mid]) return mid
10+
if (target > nums[mid]) l = mid + 1
11+
else r = mid - 1
12+
}
13+
14+
return l
15+
}
16+
}

0 commit comments

Comments
 (0)