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.python多继承 2.python经典类的MRO 3.python新式类的MRO.C3算法 4.super是什么鬼? 一.python多继承 在前⾯的学习过程中. 我们已经知道了 ...

  2. JAVA 基础--开发环境IDEA 搭建

    1.下载IDEA  (500M+) 2.激活. 在网站http://idea.lanyus.com/中获取注册码,填入Activation code中: 然后点击Activate即可. 3.创建工程前 ...

  3. PHP函数参数传递(相对于C++的值传递和引用传递)

    学语言学得比较多了,今天突然想PHP函数传递,对于简单类型(基本变量类型)和复杂类型(类)在函数参数传递时,有没有区别呢,今天测试了下: 代码如下: <?php function test($a ...

  4. web前端开发小结

    1.浏览器内核 IE-----Trident Edge-----EdgeHTML Firefox-----Gecko Safari-----Webkit Chrome-----Blink(Webkit ...

  5. python学习-- Django model -class 主键自增问题

    转自:http://blog.csdn.net/mapoor/article/details/8609660 prize_id = models.IntegerField(primary_key=Tr ...

  6. BIT+DP

    2018CCPC网络赛 J - YJJ's Salesman HDU - 6447 YJJ is a salesman who has traveled through western country ...

  7. C++ Programming with TDD之二:CppUTest单元测试

    在之前一篇C++ Programming with TDD博客中,我带给大家gmock框架的简介(地址戳着里),今天我们继续本系列,带个大家C++中的单元测试框架CppUTest的介绍. CppUTe ...

  8. xampp下bugfree部署

    以Bugfree3.0.4为例,讲解如何搭建LAMP架构的Web服务器. Bugfree是一个XAMPP架构的网站,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的搭建XAMP ...

  9. NOJ——1659求值(log10取对数+floor取整数部分+可有可无的快速幂)

    [1659] 求值 时间限制: 1000 ms 内存限制: 65535 K 问题描述 给你三个数a,b,c,求a的b次的前c位数(不够c位输出全部即可) 输入 输入数据有多组,每组占一行,有三个整数, ...

  10. POJ3349 Snowflake Snow Snowflakes 【哈希表】

    题目 很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组 题解 hash表入门题 先把一个六元组的积 + 和取模作为hash值,然后查表即可 期望\(O(n)\) #inc ...