leetcode 107. 二叉樹的層次遍歷 II 916. 單詞子集 535. TinyURL 的加密與解密

氫言發表於2020-10-01

107. 二叉樹的層次遍歷 II

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        result = []
        if root is None:
            return result
        queue = deque([root,None])
        sublist = []
        while queue:
            tmp = queue.popleft()
            if tmp is None:
                if queue:
                    queue.append(None)
                result.append(sublist.copy())
                sublist = []
                continue
            sublist.append(tmp.val)
            if tmp.left:
                queue.append(tmp.left)
            if tmp.right:
                queue.append(tmp.right)
        return result[::-1]

916. 單詞子集

class Solution:
    def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
        result = []
        # 構建兩個map
        mapA = defaultdict(dict)
        mapB = defaultdict(int) # B的組合一下,相同的字母以最多的為準
        for a in A:
            for c in a:
                mapA[a][c]=mapA[a].get(c,0)+1
        all_chr_in_B = set()
        for b in B:
            tmp=defaultdict(int)
            for c in b:
                tmp[c]+=1
                all_chr_in_B.add(c)
            for ch in all_chr_in_B:
                mapB[ch] = max(mapB[ch],tmp[ch])
        
        for val in A:
            for key,values in mapB.items():
                if mapA[val].get(key,0)<values:
                    break
            else:
                result.append(val)
        return result

535. TinyURL 的加密與解密

還沒遇見過這樣的主觀題呢。

1. map法

import re
class Codec:
    url_map = defaultdict()
    num=1
    def encode(self, longUrl: str) -> str:
        """Encodes a URL to a shortened URL.
        """
        if self.num not in self.url_map:
            self.url_map[str(self.num)]=longUrl
            self.num+=1
            return "http://tinyurl.com/"+str(self.num-1)
        return "http://tinyurl.com/"+str(self.num)
    def decode(self, shortUrl: str) -> str:
        """Decodes a shortened URL to its original URL.
        """
        return self.url_map[shortUrl.replace("http://tinyurl.com/","")]

        

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

2. 雜湊

import re
class Codec:
    url_map = defaultdict()
    def encode(self, longUrl: str) -> str:
        """Encodes a URL to a shortened URL.
        """
        if str(hash(longUrl)) not in self.url_map:
            self.url_map[str(hash(longUrl))]=longUrl
            return "http://tinyurl.com/"+str(hash(longUrl))
        return "http://tinyurl.com/"+str(self.num)
    def decode(self, shortUrl: str) -> str:
        """Decodes a shortened URL to its original URL.
        """
        return self.url_map[shortUrl.replace("http://tinyurl.com/","")]

        

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

相關文章