739. 每日温度
class Solution {
public:vector<int> dailyTemperatures(vector<int>& temperatures) {stack<int> st;int numt = temperatures.size();vector<int> out(numt, 0);for(int i=0; i<numt; ++i){while(!st.empty() && temperatures[i]>temperatures[st.top()]){out[st.top()] = i-st.top();st.pop();}st.push(i);}return out;}
};
496. 下一个更大元素 I
class Solution {
public:vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {stack<int> st;int len1 = nums1.size();int len2 = nums2.size();vector<int> out_2(len2, -1);vector<int> out_1(len1, -1);unordered_map<int, int> um;for(int i=0; i<len2; ++i){um[nums2[i]] = i;while(!st.empty() && nums2[i]>nums2[st.top()]){out_2[st.top()] = nums2[i];st.pop();}st.push(i);}for(int i=0; i<len1; ++i){if(um.count(nums1[i])!=0){out_1[i] = out_2[um[nums1[i]]];}}return out_1;}
};
503. 下一个更大元素 II
class Solution {
public:vector<int> nextGreaterElements(vector<int>& nums) {stack<int> st;int len = nums.size();vector<int> out(len, -1);for(int i=0; i<len*2; ++i){int indice = i%len;while(!st.empty() && nums[indice]>nums[st.top()]){out[st.top()] = nums[indice];st.pop();}st.push(indice);}return out;}
};