반응형
https://leetcode.com/problems/binary-search/
간단한 이진 탐색 문제였습니다.
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while(l <= r) {
int m = (l + r) / 2;
if(nums[m] == target) return m;
else if(nums[m] > target) r = m - 1;
else l = m + 1;
}
return -1;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 31 : Next Permutation (2) | 2022.03.27 |
---|---|
LeetCode 1337 : The K Weakest Rows in a Matrix (0) | 2022.03.27 |
LeetCode 881 : Boats to Save People (0) | 2022.03.24 |
LeetCode 1007 : Minimum Domino Rotations For Equal Row (0) | 2022.03.20 |
LeetCode 895 : Maximum Frequency Stack (0) | 2022.03.19 |