-
35. Search Insert Position javaLeetCode_Study_Plan/Algorithm 2022. 12. 20. 18:16
https://leetcode.com/problems/search-insert-position/?envType=study-plan&id=algorithm-i
class Solution { public int searchInsert(int[] nums, int target) { int start = 0; int end = nums.length - 1; int ans = 0; if (nums[nums.length-1] < target) { return nums.length; } else if (nums[0] > target) { return 0; } while (start <= end) { int mid = (start + end)/2; System.out.println("start: " + start + " mid: " + mid + " end: " + end); if (nums[mid] < target) { start = mid + 1; ans = start; } else if (nums[mid] == target) { ans = mid; break; } else { end = mid - 1; ans = mid; } } return ans; } }
'LeetCode_Study_Plan > Algorithm' 카테고리의 다른 글
🎵 189. Rotate Array java (0) 2022.12.20 977. Squares of a Sorted Array java (0) 2022.12.20 278. First Bad Version java (0) 2022.12.20 704. Binary Search java (0) 2022.12.20 [12.20] Algorithm 시작 (0) 2022.12.20