首页 > 其他 > 详细

栈的业务实例

时间:2019-12-28 21:33:55      阅读:64      评论:0      收藏:0      [点我收藏+]

简述

写代码思路:接到需求先要明确目标、然后分析过程(结合所学的基础知识对业务进程拆分)、逐步执行、代码实现

目标

判断字符串中的符号是否可以形成有效组合,示例:

#()\[]\{} 返回True
#([{}]) 返回True
#([)] 返回false
# (){}[] 返回True
#((]) 返回false

代码

代码块

‘‘‘
while str_raw != "":
    if ...:
    elif...:
        if...:
        else:
if stack == []:
    return True
else:
    return False
‘‘‘

代码

def check_brase(str_raw):
    if str_raw == "":
        return True

    #定义一个空列表,模拟栈。
    stack = []
    
    while str_raw != "":
        thisChar = str_raw[0]
        #如果本次循环的第一个字符是左括号,将其压栈
        if thisChar == "(" or thisChar == "{" or thisChar == "[":
            stack.append()
        elif thisChar == ")" or thisChar == "}" or thisChar == ]:
            len_stack = len(stack)
            if len_stack == 0:
                return False
            else:
                if thisChar == "(" and stack[len_stack-1] == "(":
                    stack.pop(len_stack-1)
                elif thisChar == "]" and stack[len_stack-1] == "[":
                    stack.pop(len_stack-1)
                elif thisChar == "]" and stack[len_stack-1] == "{":
                    stack.pop(len_stack-1)
                else:
                    return False
    if stack == []:
        return True
    else:
        return False
print(check_brace((){}[]{()}))

栈的业务实例

原文:https://www.cnblogs.com/wangdadada/p/12112988.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!