728x90
반응형
✨ 문제
Search Insert Position
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
https://leetcode.com/problems/search-insert-position/description/
✨ 개념
이진탐색 Binary Search
-
✨ 최종코드
class Solution {
public int searchInsert(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] >= target)
return i;
}
return nums.length;
}
}
728x90
반응형