Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f23129

Browse files
Merge pull request youngyangyang04#2242 from fwqaaq/master-3
Update 0503.下一个更大元素II.md about rust
2 parents 0bbe229 + 7aae4f8 commit 9f23129

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎problems/0503.下一个更大元素II.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ impl Solution {
293293
}
294294
```
295295

296+
> 版本二:
297+
298+
```rust
299+
impl Solution {
300+
pub fn next_greater_elements(nums: Vec<i32>) -> Vec<i32> {
301+
let (mut stack, mut res) = (vec![], vec![-1; nums.len()]);
302+
303+
for i in 0..nums.len() * 2 {
304+
while let Some(&top) = stack.last() {
305+
if nums[i % nums.len()] <= nums[top] {
306+
break;
307+
}
308+
let saved_index = stack.pop().unwrap();
309+
res[saved_index] = nums[i % nums.len()];
310+
}
311+
stack.push(i % nums.len());
312+
}
313+
314+
res
315+
}
316+
}
317+
```
296318

297319
<p align="center">
298320
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /