题解 | #下一个更大的数(二)#
下一个更大的数(二)
https://www.nowcoder.com/practice/3923970e95b140fdb65e6c00bcda403d
#include <stack>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @return int整型vector
*/
vector<int> nextBigger(vector<int>& nums) {
// write code here
stack<int> st;
int n = nums.size();
vector<int> ret(n, -1);
for(int i = 0; i < 2 * n -1; ++i){
while(!st.empty() && nums[i%n] > nums[st.top()]){
ret[st.top()] = nums[i%n];
st.pop();
}
st.push(i%n);
}
return ret;
}
};
查看5道真题和解析
