LEETCODE-DAY11

本文介绍了四个LeetCode题目,涉及栈与队列操作(如括号匹配、删除重复字符和逆波兰表达式求值),以及在Python中处理这些问题的代码实现和常见错误分析。
摘要由CSDN通过智能技术生成

title: LEETCODE-DAY11
date: 2024-03-02 17:04:05
tags:

今日题目:(栈与队列)20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值

T1

class Solution:
    def isValid(self, s: str) -> bool:
        test=list()
        for item in s:
            if item=='(':
                test.append(')')
            elif item=='[':
                test.append(']')
            elif item=='{':
                test.append('}')
            else:
                if item != test[-1]:
                    return False
                else:
                    test.pop()

        return len(test)==0

测试用例通过
s=“]”
IndexError: list index out of range
~~~~^^^^
if item != test[-1]:

class Solution:
    def isValid(self, s: str) -> bool:
        test=list()
        for item in s:
            if item=='(':
                test.append(')')
            elif item=='[':
                test.append(']')
            elif item=='{':
                test.append('}')
            else:
                if not test or item != test[-1]:
                    return False
                else:
                    test.pop()

        return len(test)==0

AC

T2

class Solution:
    def removeDuplicates(self, s: str) -> str:
        s_=[]
        for item in s:
            if item != s_[-1]:
                s_.append(item)
            else:
                s_.pop()
        return s_

IndexError: list index out of range
~~^^^^
if item != s_[-1]:

class Solution:
    def removeDuplicates(self, s: str) -> str:
        s_=[]
        for item in s:
            if s_ or item != s_[-1]:
                s_.append(item)
            elif item==s[-1]:
                s_.pop()
                
        return "".join(s_)

IndexError: list index out of range

class Solution:
    def removeDuplicates(self, s: str) -> str:
        s_=[]
        for item in s:
            if not s_ or item != s_[-1]:
                s_.append(item)
            elif item==s[-1]:
                s_.pop()
                
        return "".join(s_)

输入
s =
“abbaca”
输出
“abaca”
预期结果
“ca”

错误在于elif时已经隐含了上一处判断为false,不需要多此一举,(但理论上应该结果一样?)

class Solution:
    def removeDuplicates(self, s: str) -> str:
        s_=[]
        for item in s:
            if not s_ or item != s_[-1]:
                s_.append(item)
            if item== s[-1]:
                s_.pop()
                
        return "".join(s_)

输入
s =
“abbaca”
输出
“bc”
预期结果
“ca”

class Solution:
    def removeDuplicates(self, s: str) -> str:
        s_=[]
        for item in s:
            if not s_ or item != s_[-1]:
                s_.append(item)
            else:
                s_.pop()
                
        return "".join(s_)

AC

class Solution:
    def removeDuplicates(self, s: str) -> str:
        res = list()
        for item in s:
            if res and res[-1] == item:
                res.pop()
            else:
                res.append(item)
        return "".join(res)

if,elseif,if else区别

T3

这题思路很简单,具体实现需要学习python中lamdba函数的技巧以简化,需要仔细琢磨

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        num=list()
        for item in tokens:
            if item=='+' and len(num)>1:
                res=num[-1]+num[-2]
                num=num[:-2]
                num.append(res)
            if item=='-' and len(num)>1:
                res=num[-2]-num[-1]
                num=num[:-2]
                num.append(res)
            if item=='*' and len(num)>1:
                res=num[-2]*num[-1]
                num=num[:-2]
                num.append(res)
            if item=='/' and len(num)>1:
                res=num[-2]/num[-1]
                num=num[:-2]
                num.append(res)
            else:
                num.append(item)
        return num
            

TypeError: can’t multiply sequence by non-int of type ‘str’
~^~
res=num[-2]*num[-1]

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        num=list()
        for item in tokens:
            if item=='+' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b)+int(a)
                num.append(res)
            if item=='-' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b)-int(a)
                num.append(res)
            if item=='*' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b)*int(a)
                num.append(res)
            if item=='/' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(int(b)/int(a))
                num.append(res)
            else:
                num.append(item)
        return num

ValueError: invalid literal for int() with base 10: ‘+’
^^^^^^
res=int(b)*int(a)

再修改else处

    else:
        c=int(item)
        num.append(c)
return num


仍报错
ValueError: invalid literal for int() with base 10: ‘+’
^^^^^^^^^
c=int(item)

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        num=list()
        for item in tokens:
            if item=='+' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b)+int(a)
                num.append(res)
            if item=='-' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b)-int(a)
                num.append(res)
            if item=='*' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b)*int(a)
                num.append(res)
            if item=='/' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(int(b)/int(a))
                num.append(res)
            elif item.isdigit():
                c=int(item)
                num.append(c)
        return num


输入
tokens =
[“2”,“1”,“+”,“3”,“*”]
输出
[9]
预期结果
9

return int(num)


TypeError: int() argument must be a string, a bytes-like object or a real number, not ‘list’
^^^^^^^^
return int(num)

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        num=list()
        for item in tokens:
            if item=='+' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=b+a
                num.append(res)
            if item=='-' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=b-a
                num.append(res)
            if item=='*' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=b*a
                num.append(res)
            if item=='/' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b/a)
                num.append(res)
            if item.isdigit():
                c=int(item)
                num.append(c)
        return num[0]
            

输入
tokens =
[“3”,“-4”,“+”]

输出
3
预期结果
-1

未知bug
错误原因:

'-4'.isdigit()=False
class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        num=list()
        for item in tokens:
            if item=='+' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=b+a
                num.append(res)
            elif item=='-' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=b-a
                num.append(res)
            elif item=='*' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=b*a
                num.append(res)
            elif item=='/' and len(num)>1:
                a=num.pop()
                b=num.pop()
                res=int(b/a)
                num.append(res)
            else:
                c=int(item)
                num.append(c)
        return num[0]

通过AC

  • 35
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值