1. 232 Implement Queue using Stacks

1.1 问题描写叙述

  使用栈模拟实现队列。模拟实现例如以下操作:

  

  • push(x). 将元素x放入队尾。
  • pop(). 移除队首元素。
  • peek(). 获取队首元素。
  • empty(). 推断队列是否为空。

注意:仅仅能使用栈的标准操作,push,pop,size和empty函数。


1.2 方法与思路

   本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2。

   push(x): 将x入栈stk1.

   pop(): 依次将stk1中的元素pop到stk2中。仅仅留最后一个pop掉。然后再将stk2中的元素pop到stk1中。

   peek(): 和pop操作相似,仅仅只是最后一个元素不是pop,而是取值返回。

   empty(): 直接推断stk1是否为空就可以。

class Queue {
stack<int> stk1,stk2;
public:
// Push element x to the back of queue.
void push(int x) {
stk1.push(x);
} // Removes the element from in front of queue.
void pop(void) {
while(stk1.size() > 1)
{
stk2.push(stk1.top());
stk1.pop();
}
if(stk1.size() == 1)
stk1.pop();
while(!stk2.empty())
stk1.push(stk2.top()),stk2.pop();
} // Get the front element.
int peek(void) {
int re;
while(stk1.size() > 1)
{
stk2.push(stk1.top());
stk1.pop();
}
if(stk1.size() == 1)
re = stk1.top(); while(!stk2.empty())
stk1.push(stk2.top()),stk2.pop(); return re;
} // Return whether the queue is empty.
bool empty(void) {
return stk1.empty();
}
};

2. 231 Power of Two

2.1 问题描写叙述

  推断一个整数是不是2的幂次方。

2.2 思路与方法

  超简单的题目,题目给出的n最大为int的最大值,那么仅仅须要遍历i从1到30依次计算2的i次方是不是数n就可以。

  

class Solution {
public:
bool isPowerOfTwo(int n) {
for(int i=0; i < 31; i++)
{
if(pow(2,(double)i) == n) return true;
} return false;
}
};

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two的更多相关文章

  1. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  4. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  6. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  7. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

  8. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. 深入理解Python中的进程

    1.进程的概念什么是进程—>CPU在同一时刻只能处理一个任务,只是因为cpu执行速度很快. cpu在各个任务之间来回的进行切换. 进程的概念:正在进行的一个过程或者说一个任务,而负责执行任务的则 ...

  2. python 提交form-data之坑

    #coding=utf-8 import requests from requests_toolbelt import MultipartEncoder #requests库上传 files = {& ...

  3. 【Shell】使用shell打印菜单,一键安装Web应用

    问题描述: [解答] [root@A04-Test- scripts]# more menu.sh #!/bin/bash echo "1.[install lamp]" echo ...

  4. AtCoder Regular Contest 080

    手贱去开了abc,这么无聊.直接arc啊 C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Score : 400 points Prob ...

  5. 在Asp.net MVC中添加一个全局的异常处理的过滤器及Log4Net的使用

    1:捕获异常新建一个异常处理的类MyExceptionAttribute捕获异常信息. //写到日志中.多个线程同时操作一个文件,造成文件的并发,这时用队列 public static Queue&l ...

  6. Springmvc web项目初始化

    Web容器首先会读取项目中的web.xml配置文件中的两个节点:<context-param>与<listener> Web容器创建ServletContext对象即Servl ...

  7. CentOS7 Failed to start iptables.解决方法

    Shit, CentOS怎么这么多bug.... 公司机房周日突然掉电,之前的Openstack环境就不能用了. 重新Run了一遍安装脚本,发现这个错误: iptables 咋又起不来了呢..... ...

  8. 报错:java.lang.IllegalStateException

    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call '“refresh”' be ...

  9. linux查找文件命令

    (2)find /etc -name httpd.conf #在/etc目录下文件httpd.conf

  10. java中static变量和方法的总结

    转自:http://blog.csdn.net/haobo920/article/details/5921621 java中static变量和方法的总结 java中一切皆是对象 一个类中对象的定义一般 ...