-
今日学习的文章链接,或者视频链接
-
自己看到题目的第一想法
-
看完代码随想录之后的想法
739
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
stack<int> stk;
vector<int> result(n, 0);
stk.push(0);
for(int i = 0;i<n;i++){
if(temperatures[i]<=temperatures[stk.top()]){
stk.push(i);
}else{
while(!stk.empty()&&temperatures[i]>temperatures[stk.top()]){
result[stk.top()] = i-stk.top();
stk.pop();
}
stk.push(i);
}
}
return result;
}
};
496
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int> stk;
int n = nums1.size();
int m = nums2.size();
stk.push(nums2[0]);
vector<int> nextGreater(m,-1);
for(int i = 1;i<m;i++){
while(!stk.empty() &&nums2[i]>stk.top()){
nextGreater[find(nums2.begin(), nums2.end(), stk.top()) - nums2.begin()] = nums2[i];
stk.pop();
}
stk.push(nums2[i]);
}
map<int, int> greaterMap;
for(int i = 0;i<m;i++){
greaterMap[nums2[i]] = nextGreater[i];
}
vector<int> res(n);
for(int i = 0;i<n;i++){
res[i] = greaterMap[nums1[i]];
}
return res;
}
};
解法2(根据卡哥的解法):
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int> stk;
int n = nums1.size();
int m = nums2.size();
unordered_map<int,int> umap;
for(int i = 0;i<n;i++){
umap[nums1[i]] = i;
}
//单调栈
stk.push(0);
vector<int> result(n,-1);
for(int i = 1;i<m;i++){
while(!stk.empty()&&nums2[i]>nums2[stk.top()]){
//收获结果
if(umap.count(nums2[stk.top()])>0){
int idx = umap[nums2[stk.top()]];
result[idx] = nums2[i];
}
stk.pop();
}
stk.push(i);
}
return result;
}
};
-
自己实现过程中遇到哪些困难
-
今日收获,记录一下自己的学习时长