当前位置:网站首页>力扣刷题 每日两题(一)
力扣刷题 每日两题(一)
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
边栏推荐
- Nodejs 安装依赖cpnm时,install 出现Error: Cannot find module ‘fs/promises‘
- 免费的网络传真平台_发传真不显示发送号码
- 数据库系统原理与应用教程(073)—— MySQL 练习题:操作题 131-140(十七):综合练习
- 【R】用grafify搞定统计绘图、方差分析、干预比较等!
- 如何免费获得一个市全年的气象数据?降雨量气温湿度太阳辐射等等数据
- 使用工作队列管理器(四)
- PolarFormer: Multi-camera 3D Object Detection with Polar Transformers 论文笔记
- 基于php网上零食商店管理系统获取(php毕业设计)
- An动画基础之元件的图形动画与按钮动画
- 技术分享 | 接口自动化测试如何搞定 json 响应断言?
猜你喜欢
论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
业界新标杆!阿里开源自研高并发编程核心笔记(2022最新版)
An基本工具介绍之选择线条工具(包教会)
【精品必知】Pod生命周期
YOLOv5 training data prompts No labels found, with_suffix is used, WARNING: Ignoring corrupted image and/or label appears during yolov5 training
An动画基础之元件的图形动画与按钮动画
An introduction to basic tools for selecting line tools (package church)
(through page) ali time to upload the jar
Image fusion GAN-FM study notes
【深度学习】高效轻量级语义分割综述
随机推荐
pandas连接oracle数据库并拉取表中数据到dataframe中、筛选当前时间(sysdate)到一天之前的所有数据(筛选一天范围数据)
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,ncol参数指定行的个数、byrow参数指定按照行顺序排布图
R语言绘制时间序列的自相关函数图:使用acf函数可视化时间序列数据的自相关系数图
An基本工具介绍之选择线条工具(包教会)
欧曼自动挡、银河大马力、行星新产品 欧曼全新产品以燎原之势赢领市场
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
Apache APISIX 2.15 版本发布,为插件增加更多灵活性
Image fusion SDDGAN article learning
The common problems in the futures account summary
使用工作队列管理器(四)
从零开始C语言精讲篇5:指针
[Verilog] HDLBits Problem Solution - Verification: Writing Testbenches
实数取整写入文件(C语言文件篇)
可重入锁详解(什么是可重入)
【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
链游NFT元宇宙游戏系统开发技术方案及源码
An动画基础之散件动画原理与形状提示点
使用 %Status 值
来广州找工作有一个多月了,今天终于有着落了,工资7000
Feature Engineering Study Notes