zoukankan      html  css  js  c++  java
  • 2020-08-20

    95. 不同的二叉搜索树 II

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, val=0, left=None, right=None):
    #         self.val = val
    #         self.left = left
    #         self.right = right
    class Solution(object):
        def generateTrees(self, n):
            """
            :type n: int
            :rtype: List[TreeNode]
            """
            def find(l,r):
                if l>r: return [None]
                if l==r: return [TreeNode(l)]
                res = []
                for mid in range(l,r+1):
                    for left in find(l, mid-1):
                        for right in find(mid+1,r):
                            root = TreeNode(mid)
                            root.left = left
                            root.right = right
                            res.append(root)
                return res
            
            return find(1,n)
                        

    96. 不同的二叉搜索树

    递归+记忆化: 枚举根节点

    class Solution(object):
        def numTrees(self, n):
            """
            :type n: int
            :rtype: int
            """
            dp = [0]*(n+1)
            def dfs(now):
                if now==0 : return 1
                if dp[now]: return dp[now] 
                ans = 0
                for i in range(1, now+1):
                    ans += dfs(i-1)*dfs(now-i) 
                dp[now] = ans
                return ans
            
            return dfs(n)

    链表排序:

    插入排序:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def insertionSortList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            p = head
            while p:
                minval = p.val
                minq = p
                q = p.next
                while q:
                    if minval > q.val:
                        minval = q.val
                        minq = q
                    q = q.next
                p.val, minq.val = minq.val, p.val
                p = p.next
            
            return head

     快速排序:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def insertionSortList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            
            def partition(head ,tail):
                val = head.val
                p,q = head,head # head为基准
                while q!=tail:
                    if q.val < val:
                        p = p.next
                        p.val, q.val = q.val, p.val
                    q = q.next
                head.val, p.val = p.val, head.val
                return p
    
            def quicksort(head, tail):
                if head==tail or head.next==tail: return
                p = partition(head, tail)
                quicksort(head, p)
                quicksort(p.next, tail)
    
            quicksort(head, None)
            return head

    归并排序:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def insertionSortList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            
            def merge(L1 ,L2):
                head = ListNode(-1)
                p = head
                while L1 and L2:
                    if L1.val < L2.val:
                        p.next = L1
                        p = L1
                        L1 = L1.next
                    else:
                        p.next = L2
                        p = L2
                        L2 = L2.next
                if L1: p.next = L1
                else: p.next = L2
                return head.next
    
            def mergesort(head):
                if head is None or head.next==None: return head
                p,q,mid = head, head, head
                while q and q.next:
                    mid = p
                    p = p.next
                    q = q.next.next
                mid.next = None
                return merge(mergesort(head), mergesort(p))
    
    
            head = mergesort(head)
            return head
  • 相关阅读:
    linux-网卡故障
    css hack
    IE7的overflow失效的解决方法
    Js中 关于top、clientTop、scrollTop、offsetTop的用法
    javascript作用域(Scope),简述上下文(context)和作用域的定义
    统计代码行数的小技巧
    sql复制表、拷贝表、临时表
    string.format
    手机号正则验证
    getBoundingClientRect() 来获取页面元素的位置
  • 原文地址:https://www.cnblogs.com/liyinggang/p/13534499.html
Copyright © 2011-2022 走看看