重庆网站seo优化,成都网站优化指导,网站做百度排名教程,网站开发哪一种语言好单调栈#xff1a;
保持栈内的元素始终递增或递减。
单调递增 待处理数组{1,5,2,5,7,2,8}
public void sameyIncrease(int[] nums) {StackInteger stack new Stack();for(int i 0; i nums.length; i) {//当栈空的时候可以直接进栈或者要进栈的数大于…单调栈
保持栈内的元素始终递增或递减。
单调递增 待处理数组{1,5,2,5,7,2,8}
public void sameyIncrease(int[] nums) {StackInteger stack new Stack();for(int i 0; i nums.length; i) {//当栈空的时候可以直接进栈或者要进栈的数大于等于栈顶的数if(stack.isEmpty() || nums[stack.peek()] nums[i]) {stack.push(i);} else {//当进栈的数不满足规律的时候我们将原本栈内的数弹出直至满足规律或栈空了while(!stack.isEmpty() nums[stack.peek()] nums[i]) {stack.pop();}stack.push(i);}}
}单调递减 待处理数组{1,5,2,5,7,2,8}
public void sameyIncrease(int[] nums) {StackInteger stack new Stack();for(int i 0; i nums.length; i) {if(stack.isEmpty() || nums[stack.peek()] nums[i]) {stack.push(i);} else {while(!stack.isEmpty() nums[stack.peek()] nums[i]) {stack.pop();}stack.push(i);}}
}