两数相加(leetcode 002)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
if not l1:
return l2
if not l2:
return l1
l1.val += l2.val # 将两数相加,赋值给 l1 节点
if l1.val >= 10:
l1.next = self.addTwoNumbers(ListNode(l1.val // 10), l1.next)
l1.val %= 10
l1.next = self.addTwoNumbers(l1.next, l2.next)
return l1
最小栈(leetcode 155)
主要就是熟悉一下面向对象的用法。
class MinStack:
def __init__(self):
self.stack=[]
def push(self, val: int) -> None:
self.stack.append(val)
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return min(self.stack)
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
有效的括号(leetcode 020)
这是以前面试的时候做过的一个题目,我这里重新写一下。
class Solution:
def isValid(self, s: str) -> bool:
stack=[]
for i in s:
stack.append(i)
if len(stack)>1:
if stack[-2]+stack[-1]=='()' or stack[-2]+stack[-1]=='[]' or stack[-2]+stack[-1]=='{}':
stack.pop()
stack.pop()
return len(stack)==0
基本计算器II(leetcode 227)
class Solution:
def calculate(self, s: str) -> int:
return int(eval(s))
用栈实现队列(leetcode 232)
class MyQueue:
def __init__(self):
self.queue=[]
def push(self, x: int) -> None:
self.queue.append(x)
def pop(self) -> int:
a=self.queue[0]
self.queue=self.queue[1:]
return a
def peek(self) -> int:
return self.queue[0]
def empty(self) -> bool:
return self.queue==[]
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
最长有效括号(leetcode 032)
这个题估计还是考动态规划,算了我这里简写一下
class Solution:
def longestValidParentheses(self, s: str) -> int:
a=[-1]
ans=0
for i in range(len(s)):
if s[i]=='(':
a.append(i)
else:
a.pop()
if not a:
a.append(i)
else:
ans=max(ans,i-a[-1])
return ans
接雨水(leetcode 042)
class Solution:
def trap(self, height: List[int]) -> int:
maxl = maxr = 0
n = len(height)
list = [0] * n
for i in range(n):
maxl = max(maxl, height[i])
list[i] = maxl
for i in range(n-1,-1,-1):
maxr = max(maxr,height[i])
list[i] = min(list[i],maxr)
height[i]=list[i]-height[i]
return sum(height)
用队列实现栈(leetcode 225)
class MyStack:
def __init__(self):
self.stack=[]
def push(self, x: int) -> None:
self.stack.append(x)
def pop(self) -> int:
a=self.stack.pop()
return a
def top(self) -> int:
return self.stack[-1]
def empty(self) -> bool:
return len(self.stack)==0
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
两数之和(leetcode 001)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n=len(nums)
for i in range(n):
for j in range(i+1,n):
if nums[i]+nums[j]==target:
return [i,j]
三数之和(leetcode 015)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n=len(nums)
res=[]
if(not nums or n<3):
return []
nums.sort()
res=[]
for i in range(n):
if(nums[i]>0):
return res
if(i>0 and nums[i]==nums[i-1]):
continue
L=i+1
R=n-1
while(L<R):
if(nums[i]+nums[L]+nums[R]==0):
res.append([nums[i],nums[L],nums[R]])
while(L<R and nums[L]==nums[L+1]):
L=L+1
while(L<R and nums[R]==nums[R-1]):
R=R-1
L=L+1
R=R-1
elif(nums[i]+nums[L]+nums[R]>0):
R=R-1
else:
L=L+1
return res
缺失的第一个正数(leetcode 042)
遍历就行
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
for i in range(1,max(nums)):
if i not in nums:
return i
return max(nums)+1
最长连续序列(leetcode 128)
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums: # 如果是空列表,直接返回0
return 0
s=set(nums)
chuliguo=set() #创建一个集合来记录**处理过**的数字,如果未处理过,就加入其中
res=0
for x in nums:
do=0 # 记录每次遍历时的结果
if x in chuliguo:
continue # 如果处理过了,就直接跳过该数字
chuliguo.add(x) #未处理过的,加入处理过的这个集合
temp=x # 用一个临时变量来记录此时x的值
while x-1 in s: # 如果x-1也在s里面,那么就把他也处理了
chuliguo.add(x-1) # 把他加入处理过的集合,因为如果不加入,再次执行到它时,执行的是重复的操作
do+=1 # 这次执行的结果也要加一
x-=1 # 再去看x-1...直到x-1不再在s中
x=temp # 因为经过上述操作,我们x值已经发生了改变,此时我们要赋回原值
while x+1 in s: # 再去看x+1
chuliguo.add(x+1)
do+=1
x+=1
res=max(do,res) #在每一次遍历中,将res重新赋值
return res+1 #返回res+1,+1是因为我们在最初处理x的时候没有把他原来的数的次数加到里面
标签:return,nums,int,self,stack,打卡,050,leetcode,def
From: https://www.cnblogs.com/Mast1031/p/17997808