File tree 3 files changed +91
-0
lines changed
solution/0000-0099/0033.Search in Rotated Sorted Array
3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -168,6 +168,38 @@ var search = function (nums, target) {
168
168
};
169
169
```
170
170
171
+ ### ** Rust**
172
+
173
+ ``` rust
174
+ impl Solution {
175
+ pub fn search (nums : Vec <i32 >, target : i32 ) -> i32 {
176
+ let mut l = 0 ;
177
+ let mut r = nums . len () - 1 ;
178
+ while l <= r {
179
+ let mid = l + r >> 1 ;
180
+ if nums [mid ] == target {
181
+ return mid as i32 ;
182
+ }
183
+
184
+ if nums [l ] <= nums [mid ] {
185
+ if target < nums [mid ] && target >= nums [l ] {
186
+ r = mid - 1 ;
187
+ } else {
188
+ l = mid + 1 ;
189
+ }
190
+ } else {
191
+ if target > nums [mid ] && target <= nums [r ] {
192
+ l = mid + 1 ;
193
+ } else {
194
+ r = mid - 1 ;
195
+ }
196
+ }
197
+ }
198
+ - 1
199
+ }
200
+ }
201
+ ```
202
+
171
203
### ** ...**
172
204
173
205
```
Original file line number Diff line number Diff line change @@ -144,6 +144,38 @@ var search = function (nums, target) {
144
144
};
145
145
```
146
146
147
+ ### ** Rust**
148
+
149
+ ``` rust
150
+ impl Solution {
151
+ pub fn search (nums : Vec <i32 >, target : i32 ) -> i32 {
152
+ let mut l = 0 ;
153
+ let mut r = nums . len () - 1 ;
154
+ while l <= r {
155
+ let mid = l + r >> 1 ;
156
+ if nums [mid ] == target {
157
+ return mid as i32 ;
158
+ }
159
+
160
+ if nums [l ] <= nums [mid ] {
161
+ if target < nums [mid ] && target >= nums [l ] {
162
+ r = mid - 1 ;
163
+ } else {
164
+ l = mid + 1 ;
165
+ }
166
+ } else {
167
+ if target > nums [mid ] && target <= nums [r ] {
168
+ l = mid + 1 ;
169
+ } else {
170
+ r = mid - 1 ;
171
+ }
172
+ }
173
+ }
174
+ - 1
175
+ }
176
+ }
177
+ ```
178
+
147
179
### ** ...**
148
180
149
181
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn search ( nums : Vec < i32 > , target : i32 ) -> i32 {
3
+ let mut l = 0 ;
4
+ let mut r = nums. len ( ) - 1 ;
5
+ while l <= r {
6
+ let mid = l + r >> 1 ;
7
+ if nums[ mid] == target {
8
+ return mid as i32 ;
9
+ }
10
+
11
+ if nums[ l] <= nums[ mid] {
12
+ if target < nums[ mid] && target >= nums[ l] {
13
+ r = mid - 1 ;
14
+ } else {
15
+ l = mid + 1 ;
16
+ }
17
+ } else {
18
+ if target > nums[ mid] && target <= nums[ r] {
19
+ l = mid + 1 ;
20
+ } else {
21
+ r = mid - 1 ;
22
+ }
23
+ }
24
+ }
25
+ -1
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments