π‘ λ¬Έμ
π― νμ΄ κ³Όμ
μ λ ¬λ λ°°μ΄μμ νΉμ target
μ index
λ₯Ό λ°ννλ λ¬Έμ μ
λλ€.
μ΄λΆ νμ
μ μ΄μ©νλ©΄ μ½κ² ν΄κ²°ν μ μμ΅λλ€.
π¨π» μ½λ
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
let head = 0
let tail = nums.length - 1
let answer = -1
while (head <= tail) {
const mid = Math.floor((head + tail) / 2)
if (nums[mid] < target) {
head = mid + 1
} else if (nums[mid] > target) {
tail = mid - 1
} else {
answer = mid
break
}
}
return answer
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 108 - Convert Sorted Array to Binary Search Tree (Easy) (0) | 2023.06.06 |
---|---|
LeetCode 328 - Odd Even Linked List (Medium) (0) | 2023.05.29 |
LeetCode 438 - Find All Anagrams in a String (Medium) (0) | 2023.05.14 |
LeetCode 42 - Trapping Rain Water (Hard) (0) | 2023.04.30 |
LeetCode 238 - Product of Array Except Self (Medium) (2) | 2023.04.24 |
π¬ λκΈ