用栈构建队列
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public MyQueue() {
stack1=new Stack();
stack2=new Stack();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
//如果出栈为空,入栈的数据全部进入出栈。
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty()&&stack2.isEmpty();
}
}
接雨水
class Solution {
public int trap(int[] height) {
// write code here
Stack<Integer> stack=new Stack();//大到小
int res=0;int left=0;
//stack.push(0);
for(int i=0;i<height.length;i++){
while(!stack.isEmpty()&&height[stack.peek()]<height[i]){
int mid=stack.pop();
if (stack.isEmpty()) { // 栈为空,左边不存在最大值,无法接雨水
break;
}
if(!stack.isEmpty())
left=stack.peek();
int h=Math.min(height[left],height[i])-height[mid];
res+=h*(i-left-1);
}
stack.push(i);
}
return res;
}
}
最大矩形问题
应该增加,使得栈里面的数据能够全部出来
int heights2[]=new int[heights.length+2];
for(int i=1;i<heights2.length-1;i++){
heights2[i]=heights[i-1];
}
移掉K位数字,使得原来的数最小
class Solution {
public String removeKdigits(String num, int k) {
//小到大排列
if(k>=num.length()||num.length()==0){return "0";}
Stack<Integer> stack=new Stack();
for(int i=0;i<num.length();i++){
int now=num.charAt(i)-'0';
while(!stack.isEmpty()&&k>0&&now<num.charAt(stack.peek())-'0'){
stack.pop();
k--;
}
if(now!=0||!stack.isEmpty()){
stack.push(i);
}
}
//单调栈后还没减够
while(k>0&&!stack.isEmpty())
{
k--;
stack.pop();
}
//10,1(当now=0时,满足条件,去掉1,但now为0,且为空。)
if(stack.isEmpty())
return "0";
StringBuilder sb=new StringBuilder();
while(!stack.isEmpty())
sb.append(num.charAt(stack.pop()));
//从后往前添加所以我们要逆序
return sb.reverse().toString();
}
}
去除重复字母
不严格的单调栈
给你一个字符串 s
,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)
class Solution {
public String removeDuplicateLetters(String s) {
Stack<Character> stack=new Stack();
for (int i = 0; i < s.length(); i++) {
Character c=s.charAt(i);
if(stack.contains(c))
continue;
//返回从 i 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
while(!stack.isEmpty()&&stack.peek()>c&&s.indexOf(stack.peek(),i)!=-1)
stack.pop();
stack.push(c);
}
StringBuilder sb=new StringBuilder();
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return new String(sb.reverse());
}
}
拼接最大数
class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
int m = nums2.length;
//不用从nums1,nums2中选择,直接合并
if (n + m == k) {
return mergeTwoArr(nums1, nums2, k);
}
int[] res = new int[k];
for (int i = 0; i <= k; i++) {
//一个为i位数 另一个为k - i位数
//共同构成k位最大数
//每个组成的数长度都不能不能超过对应数组的长度
//System.out.println("i="+i);
if (i <= n && k - i <= m) {
int arr1[]=getMaxArr(nums1, i);
// for(int j=0;j<arr1.length;j++)
// System.out.print(arr1[j]);
// System.out.println();
int arr2[]=getMaxArr(nums2, k - i);
for(int j=0;j<arr2.length;j++)
System.out.print(arr2[j]);
//System.out.println();
int[] maxArr = mergeTwoArr(arr1, arr2, k);
if (compareTwoArr(maxArr, 0, res, 0)) {
res = maxArr;
}
}
}
return res;
}
//合并两个数组,得到长度为k的数组
private int[] mergeTwoArr(int[] nums1, int[] nums2, int k) {
int[] res = new int[k];
int index = 0;
int i = 0;
int j = 0;
if(nums1.length==0){
return nums2;
}
if(nums2.length==0){
return nums1;
}
while (index < k) {
if (compareTwoArr(nums1, i, nums2, j)) {
res[index] = nums1[i];
i++;
} else {
res[index] = nums2[j];
j++;
}
index++;
}
return res;
}
//比较两个数组组成的数字大小 true代表nums1组成的数大 否则nums2组成的数大
private boolean compareTwoArr(int[] nums1, int i, int[] nums2, int j) {
int n = nums1.length;
int m = nums2.length;
while (i < n && j < m && nums1[i] == nums2[j]) {
i++;
j++;
}
if (i == n) { //前面都相等 位数少的小
return false;
} else if (j == m) { //前面都相等 位数少的小
return true;
} else if (nums1[i] > nums2[j]) { //前面都相等 当前数字大于 则大
return true;
}
return false;//前面都相等 当前数字小于 则小
}
//402题
private int[] getMaxArr(int[] num, int k) {
//大到小排列
//xuyao需要移除
int xuyao=num.length-k;
System.out.println("xuyaoyichu"+xuyao);
if(xuyao==0)return num;
if(xuyao>=num.length) return new int[0];
Stack<Integer> stack=new Stack();
for(int i=0;i<num.length;i++){
int now=num[i];
while(!stack.isEmpty()&&xuyao>0&&now>num[stack.peek()]){
stack.pop();
xuyao--;
}
if(now!=0||!stack.isEmpty()){
stack.push(i);
}
}
//单调栈后还没减够
while(xuyao>0&&!stack.isEmpty())
{
xuyao--;
stack.pop();
}
//10,1(当now=0时,满足条件,去掉1,但now为0,且为空。)
if(stack.isEmpty())
return new int[0];
ArrayList<Integer>sb=new ArrayList();
while(!stack.isEmpty())
sb.add(num[stack.pop()]);
//从后往前添加所以我们要逆序
Collections.reverse(sb);
int length=sb.size();
int a[]=new int[length];
for(int i=0;i<length;i++){
a[i]=sb.get(i);
}
return a;
}
}