Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].
Example 1:
Input: s = “3[a]2[bc]” Output: “aaabcbc”
Example 2:
Input: s = “3[a2[c]]” Output: “accaccacc”
Example 3:
Input: s = “2[abc]3[cd]ef” Output: “abcabccdcdcdef”
Example 4:
Input: s = “abc3[cd]xyz” Output: “abccdcdcdxyz”
Constraints:
1 <= s.length <= 30s consists of lowercase English letters, digits, and square brackets '[]'. s is guaranteed to be a valid input.
All the integers in s are in the range [1, 300].
There are n cities connected by some number of flights. You are given an array flights where flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>] indicates that there is a flight from city from<sub>i</sub> to city to<sub>i</sub> with cost price<sub>i</sub>.
You are also given three integers src, dst, and k, return the cheapest price fromsrctodstwith at mostkstops. If there is no such route, return-1.
Example 1:
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1 Output: 200 Explanation: The graph is shown. The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
Example 2:
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0 Output: 500 Explanation: The graph is shown. The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
Constraints:
1 <= n <= 1000 <= flights.length <= (n * (n - 1) / 2) flights[i].length == 30 <= from<sub>i</sub>, to<sub>i</sub> < n from<sub>i</sub> != to<sub>i</sub>1 <= price<sub>i</sub> <= 10<sup>4</sup> There will not be any multiple flights between two cities.
0 <= src, dst, k < n * src != dst
Given an integer n, return all the structurally unique BST’s (binary search trees), which has exactlynnodes of unique values from1ton. Return the answer in any order.
Example 1:
Input: n = 3 Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
# catelan dfs (type 4) LeetCode 095 Unique Binary Search Trees II # return the root of all the possible trees defgenerateTrees(self, n: int) -> List[TreeNode]: nums = [_ + 1for _ inrange(n)] returnself.dfs4(nums, 0, n)
defdfs4(self, nums, start, end): # [start, end) if start >= end: return [None] # remember becaues we want the 2 for-loop happen res = [] for i inrange(start, end): left_root_nodes = self.dfs4(nums, start, i) # 0, 0 right_root_nodes = self.dfs4(nums, i + 1, end) # 1, 1 for left_root_node in left_root_nodes: for right_root_node in right_root_nodes: node = TreeNode(nums[i]) node.left = left_root_node node.right = right_root_node res.append(node) return res
defmerge(self, nums: List[int], start: int, mid: int, end: int): i, j, res = start, mid + 1, [] while i <= mid and j <= end: # = decides if it is stable sort if nums[i] <= nums[j]: res.append(nums[i]) i += 1 else: res.append(nums[j]) j += 1 while i <= mid: res.append(nums[i]) i += 1 while j <= end: res.append(nums[j]) j += 1 nums[start:end + 1] = res
key为子问题索引st,value为子问题的解。不含path和res因为类似于Catalan,用子问题返回结果来组成此轮结果。f(input, st, endIndex, cache) -> List
紧跟终结条件,若在cache中,返回子问题的解。
循环结束,将子问题的结果存于cache。
注意事项:
cache是一个解的集合,所以终止条件返回也需要是一个list
Python代码:
1 2 3 4 5 6 7 8 9 10 11
defdfs(self, input, cache) -> List[int]: if <终止条件>: return [] # remember to use list ifinputin cache: return cache[input] res = [] for i inrange(len(input)): anwser = self.dfs(input[:i], cache) res.append(anwser) cache[input] = res return res