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. linux中的部分宏

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  2. python multiprocessing 源码分析

    1. 文档是最先需要了解的,读完文档可能会有很多的意外的收获同时也会留下疑惑,对于一般的使用我觉得读完文档就差不多了,除非一些很有疑惑的地方你可能需要再深入的了解一下.我读文档的目的第一个就是为了找出 ...

  3. ogre3D学习基础11 -- 交换两个场景管理器

    这一节,练习一下前几次学习的内容,功能很简单,就是建立两个不同的场景管理器,当按下键盘上某个键时切换镜头. 基本框架不变,这个监听器继承了两个父类,一个是我们的老朋友ExampleFrameListe ...

  4. Selenium WebDriver- 操作浏览器的cookie

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  5. linux随笔一

    1.过滤列表输出:*表示多个字符 ll mon* 结果输出为:mongo.sh mongo_sso.sh 2.man +命令 例如:man ls    查看命令的用法 3.cd 将shell回话移动到 ...

  6. 正则表达式 去除所有非ASCII字符

    需求: 去除字符串中包含的所有外国字符 只能使用正则如下,找到包含非ASCII的记录 db=# select * from test where info ~ '[^(\x00-\x7f)]'; id ...

  7. php将数组写入到文件的三种方法

    php将数组原样写入或保存到文件有三种方法可以实现, 第一种方法是使用serialize, 第二种方法是使用print_r, 第三种方法是使用var_export, 本文章向大家介绍这三种方法是如何将 ...

  8. AtCoder Regular Contest 089

    这场一边吃饭一边打,确实还是很菜的 C - Traveling Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem ...

  9. layer弹窗在键盘按回车将反复刷新

      条件:弹窗后不做任何点击操作或者聚焦操作对于layer.load,弹出后反复按回车,load层将不断刷新,即使设置了自动消失也只有等不按回车键才会生效.对于layer iframe层有表单就更糟糕 ...

  10. iOS学习笔记36-Masonry自动布局

    一.Masonry介绍 之前我们在屏幕适配的章节中学习过AutoLayout的使用,但那都是在可视化界面上进行添加约束完成的,我们很多时候都需要在代码中使用AutoLayout约束,苹果也为我们提供了 ...