dp:
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
if nums==[]:return
llen = len(nums)
dp = [0]*(llen)
dp[0] = nums[0]
for i in range(1,llen):
if dp[i-1]<0:dp[i]=nums[i]
else:dp[i]=dp[i-1]+nums[i]
return max(dp)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if prices==[]:return 0
llen = len(prices)
dp = [0]*llen
lowb = prices[0]
for i in range(1,llen):
if prices[i]<lowb:
lowb = prices[i]
dp[i]=dp[i-1]
else:
if prices[i]-lowb > dp[i-1]:
dp[i] = prices[i]-lowb
else:
dp[i] = dp[i-1]
return dp[-1]
# 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: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list1:return list2
if not list2:return list1
v1,v2 = list1,list2
if v1.val<=v2.val:
cur = v1
v1 = v1.next
else:
cur = v2
v2 = v2.next
head = cur
while v1 and v2:
if v1.val <= v2.val:
cur.next = v1
v1 = v1.next
else:
cur.next = v2
v2 = v2.next
cur = cur.next
if v1:cur.next = v1
if v2:cur.next = v2
return head
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:return []
res = []
queue = [root]
while queue:
qlen = len(queue)
i = 0
for _ in range(len(queue)):
i += 1
tmp = queue.pop(0)
if i == qlen:res.append(tmp.val)
if tmp.left:queue.append(tmp.left)
if tmp.right:queue.append(tmp.right)
return res
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:return
cur = head
while cur and cur.next and cur.next.val == cur.val:
tmp = cur
while cur and cur.val == tmp.val:
cur = cur.next
head = cur
pre = cur
while cur:
cur = cur.next
while cur and cur.next and cur.next.val==cur.val:
tmp = cur
while cur and cur.val == tmp.val:
cur = cur.next
pre.next = cur
pre = pre.next
return head
reverse()开销好像挺大的,改成双指针了
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
l1,l2 = [],[]
cur = head
while cur:
l1.append(cur.val)
cur = cur.next
llen = len(l1)
q1, q2 = 0, llen-1
if llen%2: #奇
while q1<=q2-1:
if l1[q1]!=l1[q2]:return False
q1 += 1
q2 -= 1
else:
while q1<q2:
if l1[q1]!=l1[q2]:return False
q1 += 1
q2 -= 1
return True
快排仅仅只是加了个随机就提升很多了
topk问题再加一个topk切分优化一下,更快了
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
def qsort(list,le,ri,border):
if le<ri:
n = random.randint(le,ri)
list[le],list[n]=list[n],list[le]
cur = list[le]
l,r=le,ri
while le != ri:
while le<ri and list[ri]>=cur:
ri -= 1
list[le] = list[ri]
while le<ri and list[le]<=cur:
le += 1
list[ri] = list[le]
list[le] = cur
if border-le<k:
qsort(list,l,le,border)
qsort(list,le+1,r,border)
else:
qsort(list,le+1,r,border)
qsort(nums,0,len(nums)-1,len(nums)-1)
return nums[len(nums)-k]