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

 

posted @ 2020-08-20 13:21  樱花庄的龙之介大人  阅读(128)  评论(0编辑  收藏  举报