一、今日任务
二、有效的括号
2.1 题目:20. 有效的括号
2.2 解题过程
这是一题非常典型的栈题。
先约定用列表list模拟栈,list.append(x) 代替 push,list.pop() 代替 pop,其他的栈函数在解题过程中慢慢增加。
再应用到了一个top()函数。具体代码如下:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
if len(s) == 0:
return True
if len(s)%2 == 1:
return False
index = 0
while index < len(s):
if s[index] == '(' or s[index] == '[' or s[index] == '{':
stack.append(s[index])
elif len(stack) > 0 :
if s[index] == ')' and stack[len(stack)-1] == '(':
stack.pop()
elif s[index] == ']' and stack[len(stack)-1] == '[':
stack.pop()
elif s[index] == '}' and stack[len(stack)-1] == '{':
stack.pop()
else:
return False
else:
return False
index += 1
if len(stack) == 0:
return True
else:
return False
2.3 阅读材料改进
随想录中的思路或者说是解法更加完美,我的代码还是一如既往的缝缝补补。
以下是复现代码:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
index = 0
while index < len(s):
if s[index] == '(':
stack.append(')')
elif s[index]=='[':
stack.append(']')
elif s[index]=='{':
stack.append('}')
elif not stack or s[index] != stack[len(stack)-1]:
return False
else:
stack.pop()
index += 1
return not stack
三、删除字符串中的所有相邻重复项
3.1 题目:1047. 删除字符串中的所有相邻重复项
3.2 解题过程
class Solution(object):
def removeDuplicates(self, s):
"""
:type s: str
:rtype: str
"""
# 思路:一直与栈顶对比。直至遍历完整个字符串。
if len(s) == 0:
return s
stack = []
stack.append(s[0])
index = 1
while index < len(s):
if stack and s[index] == stack[len(stack)-1]:
stack.pop()
index += 1
elif not stack or s[index] != stack[len(stack)-1]:
stack.append(s[index])
index += 1
return ''.join(stack)
3.3 阅读材料改进
随想录提出另外一个快慢指针的思路。以下是复现代码。
class Solution(object):
def removeDuplicates(self, s):
"""
:type s: str
:rtype: str
"""
res = list(s)
slow = fast = 0
length = len(res)
while fast < length:
res[slow] = res[fast]
if slow > 0 and res[slow] == res[slow - 1]:
slow -= 1
else:
slow += 1
fast += 1
return ''.join(res[0: slow])
四、逆波兰表达式求值
4.1 题目:150. 逆波兰表达式求值
4.2 解题过程
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack_num = []
index = 0
while index < len(tokens):
temp = tokens[index]
if temp == '+':
temp1 = stack_num.pop()
temp2 = stack_num.pop()
stack_num.append(temp1 + temp2)
elif temp == '-':
temp1 = stack_num.pop()
temp2 = stack_num.pop()
stack_num.append(temp2 - temp1)
elif temp == '/':
temp1 = stack_num.pop()
temp2 = stack_num.pop()
stack_num.append(int(temp2/temp1))
elif temp == '*':
temp1 = stack_num.pop()
temp2 = stack_num.pop()
stack_num.append(temp1 * temp2)
else:
s = tokens[index]
num = 0
x = 1
index_num = len(tokens[index]) - 1
while s[index_num] != '-' and index_num >= 0:
num += (int(s[index_num])*(x))
x *= 10
index_num -= 1
if s[0] == '-':
num = num*-1
stack_num.append(num)
index += 1
return stack_num.pop()
4.3 阅读材料改进
随想录借用了许多Python的内置函数。
以下是笔记:
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
for item in tokens:
if item not in {"+", "-", "*", "/"}:
stack.append(item)
else:
first_num, second_num = stack.pop(), stack.pop()
stack.append(
int(eval(f'{second_num} {item} {first_num}')) # 重要使用!!
)
return int(stack.pop()) # 如果一开始只有一个数,那么会是字符串形式的