Merge pull request #2242 from fwqaaq/master-3

Update 0503.下一个更大元素II.md about rust
This commit is contained in:
程序员Carl
2023-09-03 16:12:24 +08:00
committed by GitHub

View File

@ -293,6 +293,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">