猪八戒网站开发wordpress获取页面的当前位置
🎈LeetCode503.下一个更大元素||
链接:503.下一个更大元素||
给定一个循环数组
nums(nums[nums.length - 1]的下一个元素是nums[0]),返回nums中每个元素的 下一个更大元素 。数字
x的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出-1。
 
public int[] nextGreaterElements(int[] nums) {Stack<Integer> st=new Stack<>();int[] result=new int[nums.length];for(int i=0;i<result.length;i++){result[i]=-1;}for(int i=0;i<=nums.length*2;i++){while(!st.isEmpty() && nums[i%nums.length]>nums[st.peek()]){result[st.peek()]=nums[i%nums.length];st.pop();}st.push(i%nums.length);}return result;} 
🎈LeetCode 42. 接雨水
链接:42.接雨水
给定
n个非负整数表示每个宽度为1的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
 
public static int trap(int[] height) {
//        单调栈Stack<Integer> st=new Stack<>();int result=0;int left=0;int right=0;int mid=0;for(int i=0;i<height.length;i++){while(!st.isEmpty() && height[i]>height[st.peek()]){right=i;mid=st.peek();st.pop();if(!st.isEmpty()){left=st.peek();}else{break;}int h=Math.min(height[right],height[left])-height[mid];int w=right-left-1;result+=h*w;}st.push(i);}return result;}