mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge pull request #1421 from destroyerngu/master
添加0704.二分查找.md Kotlin版本
This commit is contained in:
@ -613,6 +613,36 @@ public class Solution{
|
||||
}
|
||||
```
|
||||
|
||||
**Kotlin:**
|
||||
```kotlin
|
||||
class Solution {
|
||||
fun search(nums: IntArray, target: Int): Int {
|
||||
// leftBorder
|
||||
var left:Int = 0
|
||||
// rightBorder
|
||||
var right:Int = nums.size - 1
|
||||
// 使用左闭右闭区间
|
||||
while (left <= right) {
|
||||
var middle:Int = left + (right - left)/2
|
||||
// taget 在左边
|
||||
if (nums[middle] > target) {
|
||||
right = middle - 1
|
||||
}
|
||||
else {
|
||||
// target 在右边
|
||||
if (nums[middle] < target) {
|
||||
left = middle + 1
|
||||
}
|
||||
// 找到了,返回
|
||||
else return middle
|
||||
}
|
||||
}
|
||||
// 没找到,返回
|
||||
return -1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
**Kotlin:**
|
||||
|
Reference in New Issue
Block a user