Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
해석부터 안됨
예제를 보자
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
오름차순 정렬된 배열이 있고, 타겟이 존재 하면 타겟 인덱스를 구하고, 없으면 -1 반환 대충 이런 뜻인듯
Note:
- You may assume that all elements in nums are unique. // 모든 수는 유일하다?
- n will be in the range [1, 10000].
- The value of each element in nums will be in the range [-9999, 9999].
https://blog.naver.com/cncn6666/221885351821
class Solution {
public int search(int[] nums, int target) {
if(Arrays.binarySearch(nums,target) <0){
return -1;
}
return Arrays.binarySearch(nums,target);
}
}
그냥 Arrays.binarySearch 써서 풀어도 되는 거겠지..?
'Leetcode' 카테고리의 다른 글
Leetcode <Binary Search> 287. Find the Duplicate Number 53.1% Medium (0) | 2020.04.10 |
---|---|
Leetcode <Sort> 1366. Rank Teams by Votes 50.6% Medium (0) | 2020.04.10 |
Leetcode <Sort> 242. Valid Anagram 백준 6996번 애너그램 (0) | 2020.04.05 |
Leetcode 328. Odd Even Linked List 52.5% Medium (0) | 2020.04.03 |
Leetcode 621. Task Scheduler 47.7% Medium (0) | 2020.04.03 |
댓글