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)
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