力扣21~30题

21题(简单):

分析:

按要求照做就好了,这种链表基本操作适合用c写,python用起来真的很奇怪

python代码:

Definition for singly-linked list.

class ListNode:

def init(self, val=0, next=None):

self.val = val

self.next = next

class Solution:

def mergeTwoLists(self, list1: OptionalListNode, list2: OptionalListNode) -> OptionalListNode:

if list1 and list2:

p=ListNode()

head=p

while 1:

if list1==None:

p.next=list2

break

elif list2==None:

p.next=list1

break

else:

if list1.val>list2.val:

p.next=list2

p=p.next

list2=list2.next

else:

p.next=list1

p=p.next

list1=list1.next

return head.next

else:

return list1 if list1 else list2

22题(中等)

分析:

这个题明显要用递归才行,如果用循环真不好做,我们每次确定第n位,但是n+1位有2种(或一种情况),这个要丢进数组里面,下一次循环要对数组每一个进行进一步操作,能做但是很麻烦,我们知道循环适合线性的东西,没必要在非线性的情况下使用循环,除非特别需要,东西我觉得还是用着适合的地方好

python代码:

class Solution:

def generateParenthesis(self, n: int) -> Liststr:

res=\[\]

def backcall_track(s,left,right):

if left==right:

if left!=n:

s+='('

backcall_track(s,left+1,right)

else:

res.append(s)

return

else:

if left<n:

backcall_track(s+'(',left+1,right)

if right<n:

backcall_track(s+')',left , right+1)

backcall_track('',0,0)

return res

23题(困难):

分析:

真不配这个难度,因为这个直接暴力求解都行,和第一个(21题)没区别,python没有指针,初始化感觉挺麻烦,你看了我代码就发现,我的head第一个往往都是空,返回head.next,因为我不想浪费空间重新拿创建,所以我更倾向于用它给的空间,这样看起来说实话有点别扭。但是第一个的赋值要在我们弄或者建立索引单独弄也别扭,以后再补上c++的吧

python代码

Definition for singly-linked list.

class ListNode:

def init(self, val=0, next=None):

self.val = val

self.next = next

class Solution:

def mergeKLists(self, lists: ListOptional\[ListNode]) -> OptionalListNode:

p=ListNode()

res=p

if len(lists)==0:

return None

while 1:

k=0

for i in range(len(lists)):

if listsk==None:

k=i

if listsi and listsi.val<listsk.val:

k=i

if listsk==None:

break

print(k)

p.next=listsk

p=p.next

listsk=listsk.next

return res.next if res.next else None

24题:

分析:

链表基础操作

python代码:

Definition for singly-linked list.

class ListNode:

def init(self, val=0, next=None):

self.val = val

self.next = next

class Solution:

def swapPairs(self, head: OptionalListNode) -> OptionalListNode:

if head==None:

return None

p=head

res=None

if p.next!=None:

res=p.next

q=p.next.next

p.next.next=p

p.next=q

else:

return head

while p.next!=None:

if p.next.next!=None:

q=p.next.next.next

p.next.next.next=p.next

p.next=p.next.next

p.next.next.next=q

p=p.next.next

else:

break

return res

题25(困难):

分析:

有意思,但是难度不配困难。思路其实挺清晰,我们知道链表时候插入数据但是不适合查找数据,本题因为要交换k个数据就说明一定要查找数据,因为你不知道要找第几个,而是要传入k,这个时候肯定要寻求数组的帮助,我们定义一个长度为k的数组,每次拿k个,这样我们要哪个就方便了

python代码:

Definition for singly-linked list.

class ListNode:

def init(self, val=0, next=None):

self.val = val

self.next = next

class Solution:

def reverseKGroup(self, head: OptionalListNode, k: int) -> OptionalListNode:

res=ListNode(0,head)

p=res

while 1:

node_list=\[\]

flag=1

q=p

for i in range(k):

if q.next==None:

flag=0

break

else:

q=q.next

node_list.append(q)

last_node=q.next

if flag:

for i in range(k):

p.next=node_listk-i-1

p=p.next

p.next=last_node

else:

break

return res.next

题26(简单):

分析:

这个不重复,直接使用set数据类型吧,注意注意,这题还考了一个知识点:在python,可变数据类型要怎么改变实参,内置方法就行,切片不行

python代码:

class Solution:

def removeDuplicates(self, nums: Listint) -> int:

set_nums=set(nums)

new_nums=list(set_nums)

#注意好像-1在set中很大

new_nums.sort()

for i in range(len(new_nums)):

numsi=new_numsi

return len(new_nums)

题27(简单):

分析:

和上面一样,remove会改变的

python代码:

class Solution:

def removeElement(self, nums: Listint, val: int) -> int:

while val in nums:

nums.remove(val)

return len(nums)

题28(简单):

分析:

python代码:

class Solution:

def strStr(self, haystack: str, needle: str) -> int:

return haystack.find(needle)#注:这种写法比较装,以后不建议这样写,建议逐语句写

题29(中等):

分析:

可以多用gpt作为辅助,很多不知道的基础知识可以问出来

python代码:

class Solution:

def divide(self, dividend: int, divisor: int) -> int:

res = int(dividend / divisor)

if res > 2**31 - 1:

return 2**31-1

elif res < -2**31:

return -2**31

else:

return res

题30(困难):

分析:

说实话,我第一反应是找所有words的位置来减少时间复杂度,结果来一个

最后迫不得已暴力求解了

python代码:

class Solution:

def findSubstring(self, s: str, words: Liststr) -> Listint:

word_len=len(words)

word_size=len(words0)

if word_len*word_size>len(s) or word_len==0:

return \[\]

res=\[\]

def in_word(word,words):

w1=word\[word_size\*i:word_size\*(i+1) for i in range(word_len)]

w1.sort()

w2=sorted(words)

if w1==w2:

return 1

else:

return 0

for i in range(len(s)-word_size*word_len+1):

word=si:i+word_size\*word_len

if in_word(word,words):

res.append(i)

return res

总结:

我发现我都注释没怎么写过,导致写得时候思路没有特别清晰,所以要开始写注释了

相关推荐
Dillon Dong33 分钟前
【风电控制】TI TMS320F28379D 双CPU架构解析与任务分布设计
嵌入式硬件·算法·变流器·风电控制
花酒锄作田4 小时前
[python]argparse 包在聊天机器人中的应用
python
NiceCloud喜云6 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
小羊在睡觉6 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
3DVisionary6 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
AI玫瑰助手6 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
好评笔记6 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466856 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
小糖学代码7 小时前
LLM系列:环境搭建:5.Python-dotenv 环境变量管理
人工智能·python·深度学习·神经网络
sheeta19987 小时前
LeetCode 每日一题笔记 日期:2026.05.29 题目:3300. 最小元素
笔记·leetcode