LeetCode_Study_Plan/Algorithm
704. Binary Search java
개발하는루루
2022. 12. 20. 16:01
https://leetcode.com/problems/binary-search/description/?envType=study-plan&id=algorithm-i
Binary Search - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
public int search(int[] nums, int target) {
int start = 0;
int end = nums.length-1;
int idx = -1;
while (start <= end) {
int mid = (start + end)/2;
if (nums[mid] == target) {
idx = mid;
break;
}
if (target > nums[mid]) {
start = mid + 1;
} else {
end = mid -1;
}
}
return idx;
}
}
https://leetcode.com/problems/binary-search/?envType=study-plan&id=algorithm-i
Binary Search - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com