栈的实现:

 # 定义一个栈类
class Stack():
# 栈的初始化
def __init__(self):
self.items = []
# 判断栈是否为空,为空返回True
def isEmpty(self):
return self.items ==[]
# 向栈内压入一个元素
def push(self, item):
self.items.append(item)
# 从栈内推出最后一个元素
def pop(self):
return self.items.pop()
# 返回栈顶元素
def peek(self):
return self.items[len(self.items)-1]
# 判断栈的大小
def size(self):
return len(self.items)

字符串反转:

 #字符串反转
def revstring(str):
s = Stack()
outputstr = '' for i in str:
s.push(i)
while not s.isEmpty():
outputstr += s.pop() return outputstr

括号匹配:

 #括号匹配
def parCheker(str):
s = Stack()
balanced = True
index = 0
while index<len(str) and balanced:
str1 = str[index]
if str1 in '([{':
s.push(str1)
else:
if s.isEmpty():
balanced = False
else:
top = s.pop()
if not matches(top,str1):
balanced = False index += 1 if s.isEmpty() and balanced:
return True
else:
return False def matches(open,close):
opens = '([{'
closes = ')]}'
return opens.index(open) == closes.index(close)

十进制转换成二进制:

 #十进制转换为二进制
def Dec2Bin(num):
s = Stack()
while num>0:
temp = num%2
s.push(temp)
num = num // 2 binString = ''
while not s.isEmpty():
binString += str(s.pop()) return binString

参考:https://github.com/Jack-Lee-Hiter/AlgorithmsByPython/blob/master/Stack.py

python栈--字符串反转,括号匹配的更多相关文章

  1. 栈应用之 括号匹配问题(Python 版)

    栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...

  2. jzyzoj 栈——P1148:括号匹配加强版

    括号匹配加强版 描述 Description 对于一个由(,),[,]括号组成的字符串,求出其中最长的括号匹配字串. 具体来说,满足如下条件的字符串成为括号匹配的字符串: (1) (),[] 是括号匹 ...

  3. Python实现字符串反转的几种方法

    面试遇到的一个特无聊的问题--- 要求:在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 "fedcba" 第一种:使用字符 ...

  4. C数据结构-栈和队列,括号匹配举例---ShinePans

    1.栈和队列是两种特殊的线性表             运算操作被限定仅仅能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构 ...

  5. 牛客国庆集训派对Day1:J:Princess Principal(栈模拟求括号匹配)

    题目描述 阿尔比恩王国(the Albion Kingdom)潜伏着一群代号“白鸽队(Team White Pigeon)”的间谍.在没有任务的时候,她们会进行各种各样的训练,比如快速判断一个文档有没 ...

  6. leetcode题解:Valid Parentheses(栈的应用-括号匹配)

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  7. Python实现字符串反转

    将字符串 s=‘helloword’ 反转输出为 ‘drowolleh’,以下通过多种方法实现 1.字符串切片法(常用) s='helloword' r=s[::-1] print(r) #结果:dr ...

  8. [转]Python实现字符串反转的几种方法

    #第一种:使用字符串切片 result = s[::-1] #第二种:使用列表的reverse方法 l = list(s) l.reverse() result = "".join ...

  9. python之字符串反转

    def main(): a = "abcdefg" a = a[::-1] print(a) if __name__ == '__main__': main()

随机推荐

  1. 关于stm32的输入输出

    https://blog.csdn.net/u011556018/article/details/72629082

  2. 3、Pycharm使用

    1.设置文件模板 file->settings->Editor->File and Code Templates->Python Script 2.运行 a.点击要运行的文件, ...

  3. 【u247】生物进化

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一片茂密的原始森林中,生物学家们发现了几种远古时期的动物化石.他们将化石依次编号为1,2,3,--n ...

  4. 搭建hbase-0.94.26集群环境 分类: B7_HBASE 2015-01-24 17:14 861人阅读 评论(0) 收藏

    先安装hadoop1.2.1,见http://blog.csdn.net/jediael_lu/article/details/38926477 1.配置hbase-site.xml <prop ...

  5. iOS开发Quzrtz2D:十一:图片截屏以及图片擦除

    一:图片截屏:截取的是控制器的view #import "ViewController.h" @interface ViewController () @end @implemen ...

  6. css3-9 css中的浮动怎么使用

    css3-9 css中的浮动怎么使用 一.总结 一句话总结:用来做一般的行效果,比如说手机左右分布的头部导航栏.浮动的东西放到一个div中去,里面的内容根据需求左浮动或者右浮动,然后记得加上清除浮动. ...

  7. 编辑器vim简介

    vi简介 vi是"Visual interface"的简称,它在Linux上的地位就仿佛Edit程序在DOS上一样.它可以执行输出.删除.查找.替换.块操作等众多文本操作,而且用户 ...

  8. 关于 rman duplicate from active database 搭建dataguard--系列一

    关于 rman duplicate from active database.详细操作实际为backup as copy .会拷贝非常多空块.对于那些数据库数据文件超过100G的都不是非常建议用:在非 ...

  9. Methods and systems to control virtual machines

    Methods and systems are provided to control the execution of a virtual machine (VM). A VM Monitor (V ...

  10. [Flow] The Fundamentals of Flow

    Install: yarn global add flow-typed /*get type defination*/ yarn add flow-bin -D For example you hav ...