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. HDU 5238 Calculator 线段树 中国剩余定理

    题意: 给一个计算器,有一系列计算步骤,只有加,乘,幂三种运算. 有一种查询操作:查询初始值为\(x\)的时候,最终运算结果模\(29393\)的值. 有一种修改操作:可以修改第\(p\)个运算的运算 ...

  2. 装饰器与lambda

    装饰器 实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉.只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番, ...

  3. Linux IP怎么设置

    最常用的给网卡配置ip的命令为#ifconfig eth0 192.168.0.1 但是,这样重启后又打回原形.要想永久保存,需要 vim /etc/sysconfig/network-scripts ...

  4. Java EE - Servlet 3.0 和 Spring MVC

    Table of Contents 前言 基于 Java 的配置 ServletContainerInitializer 动态配置 DispatcherServlet 和 ContextLoaderL ...

  5. 《机器学习实战》笔记——AdaBoost

    笔记见备注 # _*_ coding:utf-8 _*_ from numpy import * # 简单数据集 def loadSimpData(): datMat = matrix([[1., 2 ...

  6. CodeM初赛B轮

    做什么啊,我这么菜,应该弃赛的 [编程|1500分] 子串 时间限制:3秒空间限制:32768K 题目描述 给出一个正整数n,我们把1..n在k进制下的表示连起来记为s(n,k),例如s(16,16) ...

  7. hive查询语法

    1.创建表: >create table value_data(citing INT,cited INT) >row format delimited >fields termina ...

  8. kb-07线段树-03--区间修改查询--lazy思想

    /* 区间修改,区间查询和: 第一次使用lazy思想: poj3468 */ #include<iostream> #include<cstdio> #include<c ...

  9. POJ——3061Subsequence(尺取法或二分查找)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11224   Accepted: 4660 Desc ...

  10. 理解 Glance

    OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service 先得搞清楚什么是 Image 以及为什么要用 Image? 在传统 IT 环境下, ...