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 | 31 |
Tags
- 파이썬
- Counting
- Data Structure
- Stack
- Binary Tree
- sorting
- 코테
- geometry
- array
- 구현
- SQL
- string
- Math
- Class
- java
- 자바
- Binary Search
- hash table
- greedy
- bit manipulation
- implement
- dynamic programming
- Matrix
- simulation
- database
- 코딩테스트
- Tree
- two pointers
- Method
- Number Theory
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 704. Binary Search 본문
1. Input
1) int[] nums
2) int target
2. Output
1) nums에서 target을 찾아 그 인덱스를 반환
- target이 nums에 없다면 -1을 반환
3. Constraint
1) 1 <= nums.length <= 10^4
2) - 10^4 < nums[i], target < 10^4
3) nums에는 중복된 요소가 없다.
4) nums는 오름차순으로 정렬되어 있다.
5) 알고리즘은 반드시 O(lg n)을 따라야 한다.
4. Example
Input: nums = [-1,0,3,5,9,12], target = 9 -> Output: 4
Input: nums = [-1,0,3,5,9,12], target = 2 -> Output: -1
5. Code
1) 첫 코드(2023/03/18)
int low = 0, high = nums.length-1, mid, result = -1;
while(low<=high && result==-1){
mid = (low+high)/2;
if(nums[mid]==target)
result = mid;
else if(nums[mid]<target)
low = mid + 1;
else
high = mid - 1;
}
return result;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2525. Categorize Box According to Criteria (0) | 2023.03.18 |
---|---|
[LeetCode/Easy] 705. Design HashSet (0) | 2023.03.18 |
[백준 온라인 저지] 25083. 새싹 (0) | 2023.03.17 |
[백준 온라인 저지] 10172. 개 (0) | 2023.03.17 |
[백준 온라인 저지] 10171. 고양이 (0) | 2023.03.17 |