leetcode刷题记录(六)

应该是上周刷的题一直忘记发了


763. Partition Labels

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
    def partitionLabels(self, S):
        “””
        :type S: str
        :rtype: List[int]
        “””
        size = []
        while S:
            I = 1
            while set(S[I:])&set(S[:I]):
                I+=1
            size.append(i)
            S=S[I:]
        return size

806. Number of Lines To Write String

1
2
3
4
5
6
7
8
9
10
11
12
def numberOfLines(self, widths, S):
        “””
        :type widths: List[int]
        :type S: str
        :rtype: List[int]
        “””
        res, cur = 1, 0
        for I in S:
            width = widths[ord(i) - ord(‘a’)]
            res += 1 if cur + width > 100 else 0
            cur = width if cur +width > 100 else cur + width
        return [res, cur]

537. Complex Number Multiplication

1
2
3
4
5
6
7
8
def complexNumberMultiply(self, a, b):
        “””
        :type a: str
        :type b: str
        :rtype: str
        “””
        a, ai, b, bi = map(int, re.findall(‘-?d+’, a+b))
        return ‘%d+%di’ % (a*b - ai*bi, a*bi + ai*b)

419. Battleships in a Board

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def countBattleships(self, board):
        “””
        :type board: List[List[str]]
        :rtype: int
        “””
        total = 0
        for I in range(len(board)):
            for j in range(len(board[0])):
                if board[I][j] == ‘X’:
                    flag = 1
                    if j>0 and board[I][j-1] == ‘X’:
                        flag = 0
                    if I>0 and board[i-1][j] == ‘X’:
                        flag = 0
                    total += flag
        return total

821. Shortest Distance to a Character

1
2
3
4
5
6
7
### def shortestToChar(self, S, C):
###         “””
###         :type S: str
###         :type C: str
###         :rtype: List[int]
###         “””
###         return [min(abs(I - j) for j, e in enumerate(S) if e == C) for I in range(len(S))]

530.Minimum Absolute Difference in BST

1
2
3
4
5
6
7
8
9
10
11
12
13
def getMinimumDifference(self, root):
        “””
        :type root: TreeNode
        :rtype: int
        “””
        def dsf(node, l = []):
            if node.left: dsf(node.left, l)
            l.append(node.val)
            if node.right: dsf(node.right, l)
            return l
        
        l = [1,2,3,4,5]
        return zip(l,l[1:])

669. Trim a Binary Search Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    def trimBST(self, root, L, R):
        “””
        :type root: TreeNode
        :type L: int
        :type R: int
        :rtype: TreeNode
        “””
        if not root:
            return None
        if L > root.val:
            return self.trimBST(root.right, L, R)
        elif R < root.val:
            return self.trimBST(root.left, L, R)
        root.left = self.trimBST(root.left, L, R)
        root.right = self.trimBST(root.right, L, R)
        return root

513. Find Bottom Left Tree Value

1
2
3
4
5
6
7
8
9
def findBottomLeftValue(self, root):
        “””
        :type root: TreeNode
        :rtype: int
        “””
        q = [root]
        for node in q:
            q+=filter(None, (node.right, node.left))
        return q

637. Average of Levels in Binary Tree

1
2
3
4
5
6
7
8
9
10
def dfs(node, depth = 0):
            if node:
                if len(info) <= depth:
                    info.append([0,0])
                info[depth][0] += node.val
                info[depth][1] += 1
                dfs(node.left, depth + 1)
                dfs(node.right, depth + 1)
        dfs(root)
        return [s/float(c) for s,c in info]