Skip to content

Commit aa259a7

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Create readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent a6047f8 commit aa259a7

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
Sure! Here's a markdown-formatted LeetCode-style explanation for a problem like the one you shared:
2+
3+
---
4+
5+
```markdown
6+
---
7+
comments: true
8+
difficulty: Medium
9+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0033.Search%20in%20Rotated%20Sorted%20Array/README_EN.md
10+
tags:
11+
- Array
12+
- Binary Search
13+
---
14+
15+
<!-- problem:start -->
16+
17+
# [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array)
18+
19+
[中文文档](/solution/0000-0099/0033.Search%20in%20Rotated%20Sorted%20Array/README.md)
20+
21+
## Description
22+
23+
<!-- description:start -->
24+
25+
<p>There is an integer array <code>nums</code> sorted in ascending order (with <strong>distinct</strong> values), which is rotated at an unknown pivot index <code>k</code> (0 <= k < nums.length) such that the resulting array is <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code> (0-indexed). For example, <code>[0,1,2,4,5,6,7]</code> might be rotated at pivot index <code>3</code> and become <code>[4,5,6,7,0,1,2]</code>.</p>
26+
27+
<p>Given the array <code>nums</code> after the rotation and an integer <code>target</code>, return the index of <code>target</code> if it is in <code>nums</code>, or <code>-1</code> if it is not in <code>nums</code>.</p>
28+
29+
<p>You must write an algorithm with <code>O(log n)</code> runtime complexity.</p>
30+
31+
<!-- description:end -->
32+
33+
## Solutions
34+
35+
<!-- solution:start -->
36+
37+
### Solution 1: Binary Search
38+
39+
Since the array is rotated, we can't directly apply the standard binary search. But we can still use binary search with some modifications by identifying which half of the array is properly sorted.
40+
41+
At each step:
42+
43+
- Check if the left half is sorted.
44+
- If it is, check whether the target is within the left half.
45+
- If not, search the right half.
46+
- Repeat similarly if the right half is sorted.
47+
48+
This approach ensures that with each iteration, we eliminate half the array, maintaining the logarithmic complexity.
49+
50+
Time Complexity: $O(\log n)$
51+
Space Complexity: $O(1)$
52+
53+
<!-- tabs:start -->
54+
55+
#### Python3
56+
57+
```python
58+
class Solution:
59+
def search(self, nums: List[int], target: int) -> int:
60+
left, right = 0, len(nums) - 1
61+
while left <= right:
62+
mid = (left + right) // 2
63+
if nums[mid] == target:
64+
return mid
65+
if nums[left] <= nums[mid]:
66+
if nums[left] <= target < nums[mid]:
67+
right = mid - 1
68+
else:
69+
left = mid + 1
70+
else:
71+
if nums[mid] < target <= nums[right]:
72+
left = mid + 1
73+
else:
74+
right = mid - 1
75+
return -1
76+
```
77+
78+
#### Java
79+
80+
```java
81+
class Solution {
82+
public int search(int[] nums, int target) {
83+
int l = 0, r = nums.length - 1;
84+
while (l <= r) {
85+
int mid = (l + r) / 2;
86+
if (nums[mid] == target) return mid;
87+
if (nums[l] <= nums[mid]) {
88+
if (nums[l] <= target && target < nums[mid]) {
89+
r = mid - 1;
90+
} else {
91+
l = mid + 1;
92+
}
93+
} else {
94+
if (nums[mid] < target && target <= nums[r]) {
95+
l = mid + 1;
96+
} else {
97+
r = mid - 1;
98+
}
99+
}
100+
}
101+
return -1;
102+
}
103+
}
104+
```
105+
106+
<!-- tabs:end -->
107+
108+
<!-- solution:end -->
109+
110+
<!-- problem:end -->
111+
```
112+
113+
Let me know which LeetCode problem you'd like next in this format!

0 commit comments

Comments
 (0)