일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- FIT XR
- leetcode
- 괜찮음
- 운동
- 프로젝트
- 3줄정리
- 쓰릴오브파이트
- 화상영어
- 잡생각
- 미드시청
- Writing
- Problem Solving
- 월간
- realclass
- 매일
- 스탭퍼
- English
- 만화도
- 파비최
- 영어공부
- 링피트
- 영어원서읽기
- 리얼 클래스
- 사이드
- 뭐든
- 30분
- 읽기
- 개발자
- 10분
- Daily Challenge
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.08.08 Today's Challenge 본문
https://leetcode.com/problems/search-in-rotated-sorted-array/
Search in Rotated Sorted Array - LeetCode
Can you solve this real interview question? Search in Rotated Sorted Array - There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <=
leetcode.com
이분탐색으로 풀면 되는 문제.
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
l, r = 0, n-1
while l <= r:
m = l + (r - l) // 2
if nums[m] == target:
return m
elif nums[m] >= nums[l]:
if target >= nums[l] and target < nums[m]:
r = m - 1
else:
l = m + 1
else:
if target <= nums[r] and target > nums[m]:
l = m + 1
else:
r = m - 1
return -1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.08.10 Today's Challenge (0) | 2023.08.10 |
---|---|
2023.08.09 Today's Challenge (0) | 2023.08.09 |
2023.08.07 Today's Challenge (0) | 2023.08.07 |
2023.08.06 Today's Challenge (0) | 2023.08.06 |
2023.08.05 Today's Challenge (0) | 2023.08.05 |
Comments