当前位置:网站首页>力扣刷题 每日两题(一)
力扣刷题 每日两题(一)
2022-08-03 12:45:00 【车厘子子】
一、力扣20题


class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) == 0:
return True
stack = []
for c in s:
if c == '(' or c == '[' or c == '{':
stack.append(c)
else:
if len(stack) == 0:
return False
else:
temp = stack.pop()
if c == ')':
if temp != '(':
return False
elif c == ']':
if temp != '[':
return False
elif c == '}':
if temp != '{':
return False
return True if len(stack) == 0 else False解题思路:
这道题采用栈的解法。例如“(())[]”。首先做一个从头到尾的遍历,如果遍历到是左边的符号“(”、“[”、“{”,则放到栈里边。继续往下遍历,如果遇到右边的符号“)”、“]”、“}”,则把栈里边放进去的左边的符号按照后入先出的原则取出,与右边的符号进行匹配,如果能够匹配得上,则返回True。全部遍历完之后,检查栈里还有没有符号,如果栈空就返回True,否则就返回False。
二、力扣21题

class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
res = ListNode()
cur = res
while(list1 !=None and list2 !=None):
if list1.val <= list2.val:
cur.next = list1
list1 = list1.next
else:
cur.next = list2
list2 = list2.next
cur = cur.next
cur.next = list1 or list2
return res.next边栏推荐
- 基于php志愿者服务平台管理系统获取(php毕业设计)
- How does Filebeat maintain file state?
- ECCV 2022 | AirDet: 无需微调的小样本目标检测方法
- R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,ncol参数指定行的个数、byrow参数指定按照行顺序排布图
- 基于php网上零食商店管理系统获取(php毕业设计)
- 长江商业银行面试
- GameFi 行业下滑但未出局| June Report
- 超多精美礼品等你来拿!2022年中国混沌工程调查启动
- 易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
- 【必读要点】Pod控制器Deployment更新、回退详解
猜你喜欢
随机推荐
类型转换、常用运算符
Nodejs 安装依赖cpnm时,install 出现Error: Cannot find module ‘fs/promises‘
Oracle is installed (system disk) and transferred from the system disk to the data disk
An动画基础之元件的影片剪辑动画与传统补间
图像融合SDDGAN文章学习
shell编程之条件语句
Image fusion DDcGAN study notes
Yahoo! Answers-数据集
特征降维学习笔记(pca和lda)(1)
Feature dimensionality reduction study notes (pca and lda) (1)
SQL分页查询_Sql根据某个字段分页
From the physical level of the device to the circuit level
(through page) ali time to upload the jar
免费的网络传真平台_发传真不显示发送号码
超多精美礼品等你来拿!2022年中国混沌工程调查启动
shell编程条件语句
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
可视化图表设计Cookbook
【Verilog】HDLBits题解——Verification: Reading Simulations
可重入锁详解(什么是可重入)









