Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Class
- dynamic programming
- java
- 코테
- bit manipulation
- Method
- 구현
- greedy
- string
- simulation
- 파이썬
- Tree
- Binary Tree
- hash table
- geometry
- array
- Counting
- 자바
- two pointers
- Stack
- SQL
- Matrix
- Binary Search
- sorting
- Data Structure
- implement
- Number Theory
- database
- 코딩테스트
- Math
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 35. Search Insert Position 본문
1. Input
1) int[] nums
2) int target
2. Output
1) nums에 target을 찾아서 그 인덱스를 반환
2) nums에 target이 없다면 target이 들어가게 될 인덱스를 반환
3. Constraint
1) 1 <= nums.length <= 10^4
2) - 10^4 <= nums[i] <= 10^4
3) nums에는 중복요소가 없고 오름차순으로 정렬되어 있다.
4) - 10^4 <= target <= 10^4
5) O(log n)의 복잡도를 갖는 알고리즘으로 작성해야 한다.
4. Example
Input: nums = [1,3,5,6], target = 5 -> Output: 2
Input: nums = [1,3,5,6], target = 2 -> Output: 1
Input: nums = [1,3,5,6], target = 7 -> Output: 4
5. Code
class Solution {
public int searchInsert(int[] nums, int target) {
int low = 0, high = nums.length-1;
int mid = (low+high)/2;
while(low<high){
if(target<nums[mid])
high = mid - 1;
else if(nums[mid]<target)
low = mid + 1;
else
return mid;
mid = (low+high)/2;
}
if(target<=nums[low]) return low;
else return low+1;
}
}
- 100%, 93%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 101. Symmetric Tree (0) | 2023.07.17 |
---|---|
[LeetCode/Easy] 83. Remove Duplicates from Sorted List (0) | 2023.07.17 |
[백준 온라인 저지] 1904. 01타일 (0) | 2023.07.14 |
[백준 온라인 저지] 11659. 구간 합 구하기 4 (0) | 2023.07.09 |
[백준 온라인 저지] 20920. 영단어 암기는 괴로워 (0) | 2023.07.07 |