7.27 - medium总结23

435. Non-overlapping Intervals: greedy的方法,排排序,考虑其中的逻辑,一个一个加入res。
436. Find Right Interval: 把所有值存入hash,然后用binary search来找到minimum right
439. Ternary Expression Parser: 嗯,我做成从前往后eval了,结果是从后往前eval

class Solution(object):
    def parseTernary(self, expression):
        """
        :type expression: str
        :rtype: str
        """
        s = []
        e = expression
        i = len(e) - 1
        while i >= 0:
            if e[i] == ":":
                i -= 1
            elif e[i] =="?":
                if e[i-1] == "T":
                    v = s.pop()
                    s.pop()
                    s.append(v)
                else:
                    s.pop()
                i -= 2
            else:
                s.append(e[i])
                i -= 1
        return s[0]

442. Find All Duplicates in an Array: 把负值做为已经被访问过一次的indicator
444. Sequence Reconstruction: 拓扑排序,拓扑排序还算是熟练,先抄个答案,然后总结的时候再做一遍
445. Add Two Numbers II: 可以用额外的空间来避免reverse linkedlist
449. Serialize and Deserialize BST: 利用preorder traversal来encode,然后利用bst的性质(左子树的所有节点小于root,右子树的所有节点大于root)来recover。
450. Delete Node in a BST: 我最开始做的分case1 leafnode, case2,only one child, case3 two children,不过貌似有更加简便的解法。

class Solution(object):
    def deleteNode(self, root, key):
        if not root:
            return None
        
        if root.val == key:
            if root.left:
                left_right_most = root.left
                while left_right_most.right:
                    left_right_most = left_right_most.right
                left_right_most.right = root.right
                
                return root.left
            else:
                return root.right
                
        elif root.val > key:
            root.left = self.deleteNode(root.left, key)
        else:
            root.right = self.deleteNode(root.right, key)
            
        return root

451. Sort Characters By Frequency: 用两次hash,第二次hash可以用bucket来代替。bucket初始化的时候用len+1的长度来初始化。用第一次hash的频率还做为bucket的index
452. Minimum Number of Arrows to Burst Balloons: greedy的问题,先按照start排序,然后如果下一个start在当前end内,则pop出来,并且更新一下end

你可能感兴趣的:(7.27 - medium总结23)