3.27leetcode53. 最大子数组和121. 买卖股票的最佳时机21. 合并两个有序链表199. 二叉树的右视图82. 删除排序链表中的重复元素 II234. 回文链表215. 数组中的第K个

53. 最大子数组和

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)

121. 买卖股票的最佳时机

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]

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, 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
            

199. 二叉树的右视图

 

# 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

82. 删除排序链表中的重复元素 II

# 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

234. 回文链表

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

        

215. 数组中的第K个最大元素 

快排仅仅只是加了个随机就提升很多了

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]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值