当前位置:网站首页>力扣刷题 每日两题(一)
力扣刷题 每日两题(一)
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
边栏推荐
- An工具介绍之宽度工具、变形工具与套索工具
- (through page) ali time to upload the jar
- 秋招招工作
- Comics: how do you prove that sleep does not release the lock, and wait to release lock?
- 通过点击CheckBox实现背景变换小案例
- AMS simulation
- Key points for account opening of futures companies
- An工具介绍之3D工具
- 【蓝桥杯选拔赛真题48】Scratch跳舞机游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
- shell编程之条件语句
猜你喜欢
图像融合SDDGAN文章学习
Station B responded that "HR said that core users are all Loser": the interviewer was persuaded to quit at the end of last year and will learn lessons to strengthen management
论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
ECCV 2022 | AirDet: 无需微调的小样本目标检测方法
超多精美礼品等你来拿!2022年中国混沌工程调查启动
Image fusion SDDGAN article learning
漫画:怎么证明sleep不释放锁,而wait释放锁?
基于php家具销售管理系统获取(php毕业设计)
随机推荐
使用工作队列管理器(三)
一次内存泄露排查小结
浅谈低代码平台远程组件加载方案
R语言ggplot2可视化:使用ggpubr包的ggsummarystats函数可视化箱图(通过ggfunc参数设置)、在可视化图像的下方添加描述性统计结果表格
Classes and objects (upper)
2022 年 CISO 最关心的是什么?
Key points for account opening of futures companies
安全自定义 Web 应用程序登录
类和对象(中下)
【蓝桥杯选拔赛真题48】Scratch跳舞机游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
How does Filebeat maintain file state?
__unaligned修饰指针
图像融合SDDGAN文章学习
622. 设计循环队列
Station B responded that "HR said that core users are all Loser": the interviewer was persuaded to quit at the end of last year and will learn lessons to strengthen management
Tinymce plugins [Tinymce扩展插件集合]
一些测试相关知识
Comics: how do you prove that sleep does not release the lock, and wait to release lock?
Classes and Objects (lower middle)
为冲销量下探中低端市场,蔚来新品牌产品定价低至10万?