320x100
728x90
35. Search Insert Position : https://leetcode.com/problems/search-insert-position/
이진탐색 활용
class Solution {
fun searchInsert(nums: IntArray, target: Int): Int {
var low = 0;
var high = nums.lastIndex;
while(low<=high){
var mid = (low + high)/2;
when{
target == nums[mid] -> return mid
target > nums[mid] -> low = mid + 1
else -> high = mid - 1
}
}
return low
}
}
Runtime: 204 ms, faster than 75.11% of Kotlin online submissions for Search Insert Position.
Memory Usage: 37.7 MB, less than 65.80% of Kotlin online submissions for Search Insert Position.
728x90
반응형
'코딩테스트' 카테고리의 다른 글
[kotlin] leetcode - 283. Move Zeroes (0) | 2022.05.02 |
---|---|
[kotlin] leetcode - Squares of a Sorted Array (0) | 2022.04.27 |
[kotlin] leetcode - Binary Search (0) | 2022.04.26 |
[python/연결리스트] leetcode - Add Two Numbers 문제 (0) | 2022.04.20 |
[python/Array] leetcode - two sum 문제 (0) | 2022.04.18 |