网站服务器崩了怎么办,宁波城乡建设局网站,做网站要注册商标第几类,艺术网站定制【栈】Leetcode 496 下一个更大元素I 解法1 两个单调栈解法2 ---------------#x1f388;#x1f388;题目链接#x1f388;#x1f388;------------------- 解法1 两个单调栈
两个栈进行操作#xff0c;一个栈用来遍历寻找#xff0c;一个栈用来保留 将nums2中的元素… 【栈】Leetcode 496 下一个更大元素I 解法1 两个单调栈解法2 ---------------题目链接------------------- 解法1 两个单调栈
两个栈进行操作一个栈用来遍历寻找一个栈用来保留 将nums2中的元素入栈之后遍历nums1 如果栈顶元素大于nums1[i]则记录max 如果栈顶元素小于nums1[i]则弹出栈顶元素至
创建栈StackInteger mystack new Stack(); 栈顶元素mystack.peek(); 栈顶元素弹出mystack.pop(); 栈是否为空mystack.isEmpty(); 加入栈mystack.push(); 时间复杂度O(N) 空间复杂度O(N)
class Solution {public int[] nextGreaterElement(int[] nums1, int[] nums2) {int[] result new int[nums1.length];StackInteger mystack new Stack();StackInteger tempstack new Stack();for(int i 0; inums2.length; i){mystack.push(nums2[i]);}for(int i 0; i nums1.length; i){boolean sig true;int max -1;while(sig !mystack.isEmpty()){if(nums1[i] mystack.peek()){max mystack.peek();}else if(nums1[i] mystack.peek()){sig false;while(!tempstack.isEmpty()){mystack.push(tempstack.pop());}continue;}tempstack.push(mystack.pop());}result[i] max;}return result;}
}解法2
时间复杂度O(N) 空间复杂度O(N)