#!/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. hdu 2444 二分图判断与最大匹配

    题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No ...

  2. poj 3517 约瑟夫环

    最简单的约瑟夫环,虽然感觉永远不会考约瑟夫环,但数学正好刷到这部分,跳过去的话很难过 直接粘别人分析了 约瑟夫问题: 用数学方法解的时候需要注意应当从0开始编号,因为取余会等到0解. 实质是一个递推, ...

  3. A Walk Through the Forest[HDU1142]

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. Bitset[HDU2051]

    Bitset Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  6. chrome://inspect 移动前端调试方案(Android + Chrome 实现远程调试)

    一:背景通常情况我们调试移动端页面最常用的方法就是:切换pc端浏览器的userAgent来模拟手机或其他移动设备调试页面 然后用手机打开要调试的页面 刷新页面查看调试结果 但是这就存在两个问题 在pc ...

  7. 洛谷 P1118 数字三角形游戏 Label:dfs

    题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一 ...

  8. stack, deque 和 queue的对比

    stack, deque 和 queue这三个c++的STL的数据结构很类似但又各有不同. stack是堆栈,没有迭代器,特点是后进先出.用push()将元素压入栈中,top()返回栈顶元素,pop( ...

  9. 怎样将文件夹打包为jar包或war包

    在dos命令中,找到要打包文件夹位置,输入jar -cvf 打包后的名和格式 文件夹名 如图: 在我的文件夹的位置就能看到jar包和war包了

  10. cmd命令行查看windows版本

    1.ver命令不显示sp几 C:\>ver Microsoft Windows XP [Version 5.1.2600] C:\> 08: C:\Users\Administrator& ...