leetcode-496-Next Greater Element I
来源:互联网 发布:php病毒get参数 编辑:程序博客网 时间:2024/05/22 10:43
问题
题目:[leetcode-496]
思路
简单题,仔细一点。
代码
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { int sz1 = findNums.size(); int sz2 = nums.size(); std::vector<int> ret(sz1, int()); for(int i = 0; i < sz1; ++i){ int j = 0; for(j = 0;j < sz2; ++j){ if( nums[j] == findNums[i] ) break; } if(j==sz2) ret[i] = -1; else{ int k = 0; for(k = j + 1; k < sz2; ++k){ if(nums[k] > nums[j]){ ret[i] = nums[k]; break; } } if(k==sz2) ret[i] = -1; } } return ret; }};
思路1
当然,这题有O(N)复杂度的解法。
参考这个链接[Next Greater Element]。当然这个题目本生的意思和题面有一点不同。该题是求数组中任意一个元素的下一个较大元素。
算法如下:
Method 2 (Using Stack)
Thanks to pchild for suggesting following approach.
1) Push the first element to stack.
2) Pick rest of the elements one by one and follow following steps in loop.
….a) Mark the current element as next.
….b) If stack is not empty, then pop an element from stack and compare it with next.
….c) If next is greater than the popped element, then next is the next greater element for the popped element.
….d) Keep popping from the stack while the popped element is smaller than next. next becomes the next greater element for all such popped elements
….g) If next is smaller than the popped element, then push the popped element back.
3) After the loop in step 2 is over, pop all the elements from stack and print -1 as next element for them.
简言之,挨个判断数组元素。如果栈顶比当前元素小,那么栈顶的nextGreater元素就是当前元素。继续判断栈顶和当前元素的关系,知道栈顶比当前元素大或者是栈空,将当前元素入栈即可。
挨个判断结束之后,栈中剩余元素的nextGreater元素均是-1
代码1
class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { int sz1 = findNums.size(); int sz2 = nums.size(); std::vector<int> ret(sz1, int()); if(!sz1 || !sz2) return ret; std::map<int, int> mapper; std::stack<int> stk; stk.push(nums[0]); for(int i = 1; i < sz2; ++i){ while(!stk.empty() && stk.top() < nums[i] ){ mapper[stk.top()] = nums[i]; stk.pop(); } stk.push(nums[i]); } while(!stk.empty()){ mapper[stk.top()] = -1; stk.pop(); } for(int i = 0; i < sz1; ++i){ ret[i] = mapper[findNums[i]]; } return ret; }};
- leetcode-496-Next Greater Element I
- Leetcode #496 Next Greater Element I
- leetcode[496]:Next Greater Element I
- 【LeetCode】496 Next Greater Element I
- leetcode 496 Next Greater Element I
- LeetCode 496 Next Greater Element I
- LEETCODE--Next Greater Element I
- [LeetCode] Next Greater Element I
- LeetCode:Next Greater Element I
- 【leetcode】Next Greater Element I
- LeetCode Next Greater Element I
- Next Greater Element I(leetcode)
- LeetCode-Next Greater Element I
- LeetCode 496: Next Greater Element I (下一个大的元素)
- LeetCode.496(503、556) Next Greater Element I && II && III
- 【Leetcode】496. Next Greater Element I
- leetcode -- 496. Next Greater Element I 【栈】
- 【LeetCode】496. Next Greater Element I
- NYOJ47 过河问题
- 微信开发H5 video 视频在ios可以播放但是在android手机上无法播放或者黑屏
- 18.导航
- Anaconda使用总结
- 日记:slf4j与log4j的问题记录
- leetcode-496-Next Greater Element I
- jquery拼接ajax 的json和字符串拼接
- EasyUI怎么利用onBeforeRender事件
- vc tab contrl
- 唯一的博客开通
- 串口编程前瞻-熟悉JNI、NDK以及SO库的开发
- com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK.
- hdu 1533 (最小费用流)
- CSliderCtrl 重写