Given an m x nboard of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
Input: board = [[“o”,”a”,”a”,”n”],[“e”,”t”,”a”,”e”],[“i”,”h”,”k”,”r”],[“i”,”f”,”l”,”v”]], words = [“oath”,”pea”,”eat”,”rain”] Output: [“eat”,”oath”]
Example 2:
Input: board = [[“a”,”b”],[“c”,”d”]], words = [“abcb”] Output: []
Constraints:
m == board.lengthn == board[i].length 1 <= m, n <= 12board[i][j] is a lowercase English letter. `1 <= words.length <= 3 104*1 <= words[i].length <= 10*words[i]consists of lowercase English letters.
* All the strings ofwords` are unique.
deffindWords(self, board: List[List[str]], words: List[str]) -> List[str]: ifnot board ornot board[0] ornot words: return [] trie, res = Trie(), [] for word in words: trie.insert(word)
visited = [[Falsefor _ inrange(len(board[0]))] for _ inrange(len(board))] for i inrange(len(board)): for j inrange(len(board[0])): self.dfs(board, i, j, visited, trie, trie.get_head(), '', res) returnlist(res)
defdfs(self, board, start_x, start_y, visited, trie, trie_node, path, res): if start_x < 0or start_x >= len(board) or start_y < 0or start_y >= len(board[0]): return if visited[start_x][start_y]: return trie_child = trie.search_one_node(board[start_x][start_y], trie_node) ifnot trie_child: return visited[start_x][start_y] = True path += board[start_x][start_y] if trie_child.is_end: res.append(path) trie_child.is_end = False iflen(trie_child.children) == 0and board[start_x][start_y] in trie_node.children: trie_node.children.pop(board[start_x][start_y]) for dx, dy in OFFSETS: self.dfs(board, start_x + dx, start_y + dy, visited, trie, trie_child, path, res) path = path[:-1] visited[start_x][start_y] = False iflen(trie_child.children) == 0and board[start_x][start_y] in trie_node.children: trie_node.children.pop(board[start_x][start_y])
A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com" and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
A count-paired domain is a domain that has one of the two formats "rep d1.d2.d3" or "rep d1.d2" where rep is the number of visits to the domain and d1.d2.d3 is the domain itself.
For example, "9001 discuss.leetcode.com" is a count-paired domain that indicates that discuss.leetcode.com was visited 9001 times.
Given an array of count-paired domainscpdomains, return an array of the count-paired domains of each subdomain in the input. You may return the answer in any order.
Example 1:
Input: cpdomains = [“9001 discuss.leetcode.com”] Output: [“9001 leetcode.com”,”9001 discuss.leetcode.com”,”9001 com”] Explanation: We only have one website domain: “discuss.leetcode.com”. As discussed above, the subdomain “leetcode.com” and “com” will also be visited. So they will all be visited 9001 times.
Example 2:
Input: cpdomains = [“900 google.mail.com”, “50 yahoo.com”, “1 intel.mail.com”, “5 wiki.org”] Output: [“901 mail.com”,”50 yahoo.com”,”900 google.mail.com”,”5 wiki.org”,”5 org”,”1 intel.mail.com”,”951 com”] Explanation: We will visit “google.mail.com” 900 times, “yahoo.com” 50 times, “intel.mail.com” once and “wiki.org” 5 times. For the subdomains, we will visit “mail.com” 900 + 1 = 901 times, “com” 900 + 50 + 1 = 951 times, and “org” 5 times.
Constraints:1 <= cpdomain.length <= 100 1 <= cpdomain[i].length <= 100cpdomain[i] follows either the "rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>.d3<sub>i</sub>" format or the "rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>" format. rep<sub>i</sub> is an integer in the range [1, 10<sup>4</sup>].
d1<sub>i</sub>, d2<sub>i</sub>, and d3<sub>i</sub> consist of lowercase English letters.
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that node u is adjacent to. More formally, for each v in graph[u], there is an undirected edge between node u and node v. The graph has the following properties:
There are no self-edges (graph[u] does not contain u).
There are no parallel edges (graph[u] does not contain duplicate values). If v is in graph[u], then u is in graph[v] (the graph is undirected).
The graph may not be connected, meaning there may be two nodes u and v such that there is no path between them.
A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B.
Return trueif and only if it is bipartite.
Example 1:
Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]] Output: false Explanation: There is no way to partition the nodes into two independent sets such that every edge connects a node in one and a node in the other.
Example 2:
Input: graph = [[1,3],[0,2],[1,3],[0,2]] Output: true Explanation: We can partition the nodes into two sets: {0, 2} and {1, 3}.
Constraints:
graph.length == n1 <= n <= 100 0 <= graph[u].length < n0 <= graph[u][i] <= n - 1 graph[u] does not contain u.
All the values of graph[u] are unique. * If graph[u] contains v, then graph[v] contains u.
defisBipartite(self, graph: List[List[int]]) -> bool: node_to_color = collections.defaultdict(int) for i inrange(len(graph)): if i in node_to_color: # disconnected nodes continue node_to_color[i] = 0 ifnotself.bfs(graph, i, node_to_color): returnFalse returnTrue
defbfs(self, graph, n, node_to_color): queue = collections.deque([n]) while queue: node = queue.popleft() for neighbor in graph[node]: if neighbor in node_to_color and node_to_color[neighbor] != 1 - node_to_color[node]: returnFalse if neighbor in node_to_color: continue queue.append(neighbor) node_to_color[neighbor] = 1 - node_to_color[node] returnTrue
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists.
The depth of an integer is the number of lists that it is inside of. For example, the nested list [1,[2,2],[[3],2],1] has each integer’s value set to its depth.
Return the sum of each integer innestedListmultiplied by its depth.
Example 1:
Input: nestedList = [[1,1],2,[1,1]] Output: 10 Explanation: Four 1’s at depth 2, one 2 at depth 1. 12 + 12 + 21 + 12 + 12 = 10.
Example 2:
Input: nestedList = [1,[4,[6]]] Output: 27 Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3. 11 + 42 + 63 = 27.
Example 3:
Input: nestedList = [0] Output: 0
Constraints:
1 <= nestedList.length <= 50 The values of the integers in the nested list is in the range [-100, 100]. The maximum *depth of any integer is less than or equal to 50.
题目大意:
求NestedInteger的和。越深,权重越高
最后计算权重解题思路(推荐):
BFS按层遍历
Nested List题目: LeetCode 341 Flatten Nested List Iterator Iterator - Stack LeetCode 339 Nested List Weight Sum - BFS LeetCode 364 Nested List Weight Sum II - BFS
解题步骤:
N/A
注意事项:
Line 9中,需要将NestedInteger展开,里面的所有的NestedInteger入列。Python中,用extend来加入list中所有元素到另一个list,而不是append
按层遍历模板中,不需要level变量,for可以达到。
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defdepthSum(self, nestedList) -> int: queue = collections.deque(nestedList) sums, max_depth, res = [], 0, 0 while queue: layer_sum = 0 for _ inrange(len(queue)): node = queue.popleft() if node.isInteger(): layer_sum += node.getInteger() else: queue.extend(node.getList()) # remember sums.append(layer_sum) max_depth += 1 for i, n inenumerate(sums): res += n * (i + 1) return res