35. Search Insert Position

양성준·2025년 7월 14일

코딩테스트

목록 보기
92/102

문제

https://leetcode.com/problems/search-insert-position/description/

풀이

class Solution {
    public int searchInsert(int[] nums, int target) {
        int lt = 0;
        int rt = nums.length-1;
        int mid = (lt + rt) / 2;

        while(lt <= rt) {
            if(nums[mid] < target) {
                lt = mid + 1;
            }
            if(nums[mid] > target) {
                rt = mid - 1;
            }
            if(nums[mid] == target) {
                return mid;
            }
            mid = (lt + rt) / 2;
        }
        return lt;
    }
}
profile
백엔드 개발자

0개의 댓글