Update 0503.下一个更大元素II.md about rust

This commit is contained in:
fwqaaq
2023-08-21 16:29:16 +08:00
committed by GitHub
parent 49e1ab2b57
commit 7aae4f81bd

View File

@ -289,6 +289,28 @@ impl Solution {
}
```
> 版本二:
```rust
impl Solution {
pub fn next_greater_elements(nums: Vec<i32>) -> Vec<i32> {
let (mut stack, mut res) = (vec![], vec![-1; nums.len()]);
for i in 0..nums.len() * 2 {
while let Some(&top) = stack.last() {
if nums[i % nums.len()] <= nums[top] {
break;
}
let saved_index = stack.pop().unwrap();
res[saved_index] = nums[i % nums.len()];
}
stack.push(i % nums.len());
}
res
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">