#!/usr/bin/python
# -*- coding: utf-8 -*-
class A:
arr=[]
@classmethod
def push(cls,i):
cls.arr=[i]+cls.arr
@classmethod
def pop(cls):
ret=cls.arr[0]
cls.arr=cls.arr[1:]
return ret
A.arr=[66,661,662,663]
for i in range(10):
A.push(i)
import pprint
pprint.pprint(A.arr)
ret=A.pop()
pprint.pprint(A.arr)

  

小结:

1、

借助linkedlist,每次添加元素后,反转,取逆序

Implement Stack using Queues - LeetCode
https://leetcode.com/problems/implement-stack-using-queues/solution/

Implement Stack using Queues - LeetCode Articles
https://leetcode.com/articles/implement-stack-using-queues/

使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to backpeek/pop from frontsize, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

Approach #1 (Two Queues, push - O(1), pop O(n))

Approach #2 (Two Queues, push - O(n), pop O(1) )

Approach #3 (One Queue, push - O(n), pop O(1))

package leetcode;

import java.util.LinkedList;
import java.util.Queue; class MyStack { //one Queue solution
private Queue<Integer> q = new LinkedList<Integer>(); public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(-2);
myStack.push(0);
myStack.push(-3);
myStack.push(13);
myStack.pop();
myStack.top();
} // Push element x onto stack.
public void push(int x) {
q.add(x);
for (int i = 1; i < q.size(); i++) { //rotate the queue to make the tail be the head
q.add(q.poll());
}
} // Removes the element on top of the stack.
public int pop() {
return q.poll();
} // Get the top element.
public int top() {
return q.peek();
} // Return whether the stack is empty.
public boolean empty() {
return q.isEmpty();
}
}
class MyStack:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.myqueue=[] def push(self, x: int) -> None:
"""
Push element x onto stack.
""" self.myqueue=[x]+self.myqueue def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
ret=self.myqueue[0]
self.myqueue=self.myqueue[1:]
return ret def top(self) -> int:
"""
Get the top element.
"""
ret=self.myqueue[0]
return ret def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return len(self.myqueue)==0 # Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()

  

Fingerprinting的更多相关文章

  1. 帆布指纹识别(canvas fingerprinting)

    广告联盟或许网站运营者都希望能够精准定位并标识每一个个体,通过对用户行为的分析(浏览了哪些页面?搜索了哪些关键字?对什么感兴趣?点了哪些按钮?用了哪些功能?看了哪些商品?把哪些放入了购物车等等),为用 ...

  2. MaLoc: a practical magnetic fingerprinting approach to indoor localization using smartphones

    https://www.indooratlas.com/ MaLoc: a practical magnetic fingerprinting approach to indoor localizat ...

  3. Magnetic Fingerprinting Approach to Indoor Localization

    Magnetic Fingerprinting Approach to Indoor Localization

  4. DNA fingerprinting|haplotpe|frequency of polymorphism|限制性标记的多态性

    5.4利用RFLP和SNP绘制遗传图 因为限制性标记可以确定那个分子水平上的突变(即已知基因座),但是无法和蛋白质功能相联系.所以我们采用限制性标记的多态性,即该限制酶识别的位点若发生突变,则大概率在 ...

  5. HTML5 + JS 网站追踪技术:帆布指纹识别 Canvas FingerPrinting Universally Unique Identifier,简称UUID

    1 1 1 HTML5 + JS  网站追踪技术:帆布指纹识别 Canvas FingerPrinting 1 一般情况下,网站或者广告联盟都会非常想要一种技术方式可以在网络上精确定位到每一个个体,这 ...

  6. HTML5 & canvas fingerprinting

    HTML5 & canvas fingerprinting demo https://codepen.io/xgqfrms/full/BaoMWMp window.addEventListen ...

  7. 《Shazam It! Music Recognition Algorithms, Fingerprinting, and Processing》译文

    最近看到一篇老外写的博客,简单介绍了shazam的工作原理.图非常好,所以就把它翻译成中文,希望对搞听歌识曲的人有帮助. 你可能遇到这样的场景:在酒吧或者餐厅听到你非常熟悉的歌,也许你曾经听过无数次, ...

  8. Gathering Fingerprinting

    1. Banner grabbing with Netcat Netcat is multipurpose networking tool that can be used to perform mu ...

  9. 设备指纹(Device Fingerprinting)是什么?

    简单来讲,设备指纹是指可以用于标识出该设备的设备特征或者独特的设备标识.设备指纹因子通常包括计算机的操作系统类型,安装的各种插件,浏览器的语言设置及其时区 .设备的硬件ID,手机的IMEI,电脑的网卡 ...

随机推荐

  1. [译]SQL Server 之 查询计划的简单参数化

    SQL Server能把一些常量自动转化为参数,以重用这些部分的查询计划. SELECT FirstName, LastName, Title FROM Employees WHERE Employe ...

  2. 使用SQL Server维护计划实现数据库定时自动备份

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员每天守到晚上1点去备份数据库.要实现数据库的定 ...

  3. 很多k线形态或k线组合是需要验证的

    1.十字星:表明多空力量平衡.是否翻转,需要验证. 2.要反应市场心理的k线才是有效的K线,不然就是伪k或伪k线形态.

  4. 单元测试框架-TestNG的安装

    一.在eclipse中安装TestNG插件 1)打开eclipse,选择help--> Install New Software

  5. ClassLoader类加载机制

    一.类加载器 类加载器(ClassLoader),顾名思义,即加载类的东西.在我们使用一个类之前,JVM需要先将该类的字节码文件(.class文件)从磁盘.网络或其他来源加载到内存中,并对字节码进行解 ...

  6. 2016.5.27 PHP连接数据库与查询

    提示mysql_query() expects parameter 2 to be resource, null given in 错误是因为你没有连接数据库就进行了查询,正常的程序应该这样写: fu ...

  7. 在JavaScript中,this关键字指什么?

    指向对象.window.方法. 例子1 function a(){//当前调用栈是a,因此a的调用位置是全局作用域 console.log('a'); b();// b的调用位置 } function ...

  8. 【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3585 好神的题. 但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!! ...

  9. TAT,我的LCT转双旋了

    这里是rotate和splay函数 void rotate(int x) { ]==x; ch[y][d^]=ch[x][d];pre[ch[x][d]]=y; ch[z][ch[z][]==y]=x ...

  10. ubuntu 14.04 https 形式安装docker 私有库 harbor

    起始目录/root,root 登陆后,直接在该目录进行下面的命令 下载harbor 预编译包 0.4.5 准备通过域名 reg.server.com 来访问镜像库所以需要在/etc/hosts 文件中 ...