011 盛最多水的容器
11.盛最多水的容器(中等)
双指针
class Solution:
def maxArea(self, height: List[int]) -> int:
if len(height)<2:
return 0
l = 0
r = len(height)-1
max_area=0
while l<r:
lh = height[l]
rh = height[r]
if lh<rh:
max_area = max(max_area,(r-l)*lh)
l += 1
else:
max_area = max(max_area,(r-l)*rh)
r -= 1
return max_area
014 最长公共前缀
14.最长公共前缀(简单)
方法一:遍历,逐个和第一个字符串求公共前缀
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
s1 = strs[0]
res = len(s1)
for i in range(1,len(strs)):
cp = self.CommonPrefix(s1,strs[i])
if cp==0:
return ""
res = min(res,cp)
return s1[:res]
def CommonPrefix(self,s1,s2):
p = 0
n = min(len(s1),len(s2))
while p<n:
if s1[p]!=s2[p]:
break
p += 1
return p
方法二:按字典序先进行排序,计算第一个和最后一个的公共前缀即可
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
strs.sort()
s1 = strs[0]
s2 = strs[-1]
p = 0
n = min(len(s1),len(s2))
while p<n:
if s1[p]!=s2[p]:
break
p += 1
return s1[:p]
015 三数之和
15.三数之和(中等)
题目要求不可以包含重复的三元组,要跳过相同的元素
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums)<3:
return []
nums.sort()
res = []
for i in range(len(nums)):
if nums[i]>0:
break
if i>0 and nums[i]==nums[i-1]:
continue
t = 0-nums[i]
l = i+1
r = len(nums)-1
while l<r:
if nums[l]+nums[r]==t:
res.append([nums[i],nums[l],nums[r]])
while l<r and nums[l]==nums[l+1]:
l += 1
while l<r and nums[r]==nums[r-1]:
r -= 1
l += 1 ######
r -= 1 ######
elif nums[l]+nums[r]<t:
l += 1
else:
r -= 1
return res
001 两数之和
相似题目
1. 两数之和(简单)
方法一:暴力
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
if n<2:
return []
for i in range(0,n):
for j in range(i+1,n):
if nums[i]+nums[j]==target:
return [i,j]
return []
方法二:方法一的优化,使用字典保存nums[i]和i的关系
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
if n<2:
return []
seen = {}
for i in range(0,n):
t = target-nums[i]
if t in seen:
return [seen[t],i]
seen[nums[i]] = i
return []
方法三:先排序再双指针
注意:最终需要返回的是原数组中的下标,因此需要保存该信息
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
if n<2:
return []
nums = list(enumerate(nums))
nums.sort(key=lambda x:x[1])
l = 0
r = n-1
while l<r:
if nums[l][1]+nums[r][1]==target:
return [nums[l][0],nums[r][0]]
elif nums[l][1]+nums[r][1]>target:
r -= 1
else:
l += 1
return []
016 最接近的三数之和
16.最接近的三数之和(中等)
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
n = len(nums)
if n<3:
return []
res = 9999
nums.sort()
for i in range(n):
l = i+1
r = n-1
while l<r:
s = nums[i]+nums[l]+nums[r]
if s==target:
return s
else:
if abs(s-target)<abs(res-target):
res = s
if s>target:
r -= 1
else:
l += 1
return res
020 有效的括号
20.有效的括号(简单)
class Solution:
def isValid(self, s: str) -> bool:
if len(s)%2==1:
return False
stack = []
dic = {')':'(',']':'[','}':'{'}
left = ['(','{','[']
for c in s:
if c in left:
stack.append(c)
else:
if not stack:
return False
elif dic[c]!=stack.pop():
return False
return not stack
021 合并两个有序链表
21.合并两个有序链表(简单)
# 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, l1: ListNode, l2: ListNode) -> ListNode:
if not l1:
return l2
if not l2:
return l1
dummyhead = ListNode(-1)
p = dummyhead
while l1 and l2:
if l1.val<=l2.val:
p.next = l1
l1 = l1.next
else:
p.next = l2
l2 = l2.next
p = p.next
if l1:
p.next = l1
if l2:
p.next = l2
return dummyhead.next
参考资料:
Datawhale资料
https://leetcode-cn.com/problems/longest-common-prefix/solution/duo-chong-si-lu-qiu-jie-by-powcai-2/
https://leetcode-cn.com/problems/3sum/solution/pai-xu-shuang-zhi-zhen-zhu-xing-jie-shi-python3-by/