#!/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. (六)WebRTC手记之WebRtcVideoEngine2模块

    转自:http://www.cnblogs.com/fangkm/p/4401143.html 终于讲到视频数据的编码发送模块了,不容易.总体来说也看了不少时间WebRTC的源码了,最大的感触就是各个 ...

  2. Java判断文件编码格式

    转自:http://blog.csdn.net/zhangzh332/article/details/6719025 一般情况下我们遇到的文件编码格式为GBK或者UTF-8.由于中文Windows默认 ...

  3. HDU 4341 分组背包

    B - Gold miner Time Limit:2000MS      Memory Limit:32768KB     Description Homelesser likes playing ...

  4. C# 中Datetime类用法总结

    收集了一些记录下来,这些有的是从网上找的,有些是自己使用到的: DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25dt.ToF ...

  5. [转]说说C语言运算符的“优先级”与“结合性”

    补充自己的一点理解: 1.关于++i 与 i++的区别. ++i 和 i++如果是单独使用的语句,即二者后面均加上分号,或者其他单独使用的语句,没有任何区别.例如: for(i=0;i<100; ...

  6. 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea

    题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /******* ...

  7. Google不支持小于12px字体 终极办法

    每个浏览器厂商都会自己有设计的主观性,而这些出发点看似很好的却往往深深地伤害程序员. 1.需求 呈现指定为Google浏览器,字号为6-8px(为了打印细小的尺寸). 2.探索 2.1 CSS HAC ...

  8. 【wikioi】1222 信与信封问题(二分图+特殊的技巧)

    http://wikioi.com/problem/1222/ 一开始我就想到这样构图的,即可能的连边.但是似乎无法判断. 然后想来想去想不出来.. 题解: 同样是二分图,将可能的连边,然后跑一次最大 ...

  9. 李洪强-C语言7-C语言运算符

    C语言运算符 一.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. ①. 加法:+ 还可以表示正号 ②. 减法:- 还可以表示负号 ③. 乘法:* 非数学意义上的X ④. 除法:/  注意 ...

  10. Solve error: 'class vtkImageActor' has no member named 'SetInput'

    Replacement of SetInput() with SetInputData() and SetInputConnection() someFilter->SetInput(someR ...