当前位置: 首页> 游戏> 单机 > 房产网签流程图_重庆网站建设公司价钱_友链购买_头条搜索站长平台

房产网签流程图_重庆网站建设公司价钱_友链购买_头条搜索站长平台

时间:2025/7/13 6:03:47来源:https://blog.csdn.net/yqq962464/article/details/142365865 浏览次数:0次
房产网签流程图_重庆网站建设公司价钱_友链购买_头条搜索站长平台

题目

用栈来实现队列

思路1

入队直接入,出队用两个栈来回倒腾。

static class StackToQueue{Stack<Integer> stack = new Stack<>();Stack<Integer> tmpStack = new Stack<>(); //临时栈public StackToQueue(){}//入队 直接入public void enqueue(Integer val){stack.push(val);}//出队public Integer dequeue(){if (stack.isEmpty()) return null;while (!stack.isEmpty()){tmpStack.push(stack.pop());}Integer pop = tmpStack.pop();while (!tmpStack.isEmpty()){stack.push(tmpStack.pop());}return pop;}
}

思路2

入队来回倒腾,出队直接出。

static class StackToQueue2{Stack<Integer> stack = new Stack<>();Stack<Integer> tmpStack = new Stack<>(); //临时栈public StackToQueue2(){}//入队 两个栈倒腾public void enqueue(Integer val){//新元素进来时,把stack里面的元素倒腾到tmp里去while (!stack.isEmpty()){tmpStack.push(stack.pop());}stack.push(val);while (!tmpStack.isEmpty()){stack.push(tmpStack.pop());}}//出队public Integer dequeue(){if (stack.isEmpty()) return null;return stack.pop();}
}

思路3

倒腾到tmpStack后,不用再倒腾回去了;当tmpStack不为空的时候,直接从tmpStack出队。

static class StackToQueue3{Stack<Integer> stack = new Stack<>();Stack<Integer> tmpStack = new Stack<>(); //临时栈public StackToQueue3(){}//入队 直接入public void enqueue(Integer val){stack.push(val);}//出队 优化一下public Integer dequeue(){if (stack.isEmpty() && tmpStack.isEmpty()) return -1;int r;if (!tmpStack.isEmpty()){r = tmpStack.pop();} else {while (!stack.isEmpty()){tmpStack.push(stack.pop());}r = tmpStack.pop();}return r;}
}
关键字:房产网签流程图_重庆网站建设公司价钱_友链购买_头条搜索站长平台

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: