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. UVa 11795 状压DP Mega Man's Mission

    kill[S]表示消灭机器人的集合为S,剩下的所能杀死的机器人集合. 设d(S)表示杀死机器人集合为S的方法数,答案为d((1<<n) - 1). d(S)可以由d(S')转移过来,其中S ...

  2. 【Beta】Scrum meeting 2

    第一天:2019/6/25 前言: 第1次会议在6月日25由PM在教10-101召开. 明确所有任务要求,根据每个人的特长和项目需求分发任务,并明确项目前进方向.时长50min. 本日任务完成情况 成 ...

  3. 转:Generating PDFs from Web Pages on the Fly with jsPDF

    The Portable Document Format has been one the major innovations in the fields of desktop publishing ...

  4. 微信小程序开发 -- 手机振动

    wx.vibrateLong(OBJECT) wx.vibrateLong(OBJECT) 方法使手机发生较长时间的振动(400ms) OBJECT参数说明: 参数名 类型 必填 说明 success ...

  5. WIN下C开发环境搭建

    安装编译器 MinGW提供了一套简单方便的Winodows下的基于GCC程序开发环境 官网下载安装 http://www.mingw.org/ 打开后选择basic setup的package Ins ...

  6. 在Notepad++里配置python环境

    首先在语言里选择Python 然后点击运行,在弹出的对话框里输入: cmd /k cd /d "$(CURRENT_DIRECTORY)" &  python " ...

  7. 设计模式(一)单例模式:3-静态内部类模式(Holder)

    思想: 相比于懒汉以及饿汉模式,静态内部类模式(一般也被称为 Holder)是许多人推荐的一种单例的实现方式,因为相比懒汉模式,它用更少的代码量达到了延迟加载的目的. 顾名思义,这种模式使用了一个私有 ...

  8. 【Luogu】P2495消耗战(虚树DP)

    题目链接 我虚树没很理解啊qwq 就是我们有比较少的询问点,然后我们把不需要考虑的点搞一搞扔掉,然后每次询问给那些询问点单独建一颗树,然后乱搞. ……好吧看来是完全没理解…… 链接大法qwq #inc ...

  9. CTSC 1999 家园 【网络流24题】星际转移

    直接把每一个点,每一天拆成一个点. 然后每个点到下一天连$inf$的边. 然后把飞船的路径用容量为飞船容量的边连接. 然后跑网络流判断是否满流. #include <queue> #inc ...

  10. Android2.2源码属性服务分析

    属性服务property service 大家都知道,在windows中有个注册表,里面存储的是一些键值对.注册表的作用就是:系统或者应用程序将自己的一些属性存储在注册表中,即使系统或应用程序重启,它 ...