LeetCode之Search Insert Position(Kotlin)

问题:



方法:
题目要求O(logn)复杂度,直观想到使用二分查找,剩下的就是处理边界条件了

package com.eric.leetcode

class SearchInsertPosition {
    fun searchInsert(nums: IntArray, target: Int): Int {
        var start = 0
        var end = nums.lastIndex
        while (true) {
            val mid = (start + end) / 2
            if (nums[mid] > target) {
                if (mid == 0) {
                    return 0
                } else if (nums[mid - 1] < target) {
                    return mid - 1
                }
                end = mid - 1
            } else if (nums[mid] < target) {
                if (mid == nums.lastIndex) {
                    return nums.lastIndex + 1
                } else if (nums[mid + 1] > target) {
                    return mid + 1
                }
                start = mid + 1
            } else {
                break
            }
        }
        return (start + end) / 2
    }
}

fun main() {
    val nums = intArrayOf(1, 3)
    val target = 2
    val searchInsertPosition = SearchInsertPosition();
    searchInsertPosition.searchInsert(nums, target)
}

有问题随时沟通

具体代码实现可以参考Github

你可能感兴趣的:(LeetCode之Search Insert Position(Kotlin))