<div>Given the root of a binary tree, flatten the tree into a "linked list": * The "linked list" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. * The "linked list" should be in the same order as a pre-ordertraversal of the binary tree. Example 1:
Constraints: * The number of nodes in the tree is in the range [0, 2000]. * -100 <= Node.val <= 100Follow up: Can you flatten the tree in-place (with O(1) extra space)?</div>
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
Example 1:
<pre>Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.</pre>
Example 2:
<pre>Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
</pre>
Example 3:
<pre>Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
</pre>
Constraints:
2 <= k <= 9
1 <= n <= 60
</div>
题目大意:
数字1-9的组合个数为k的组合和等于k,每个元素最多用一次
解题思路:
用组合模板,先排序
解题步骤:
N/A
注意事项:
Leetcode 40和77的结合。个数和target都要达到。用if k == 0 and target == 0
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defcombinationSum3(self, k: int, n: int) -> List[List[int]]: nums = [_ for _ inrange(1, 10)] res = [] self.dfs(nums, 0, k, n, [], res) return res
defdfs(self, nums, start, k, target, path, res): if k == 0and target == 0: res.append(list(path)) return if k == 0: return for i inrange(start, len(nums)): path.append(nums[i]) self.dfs(nums, i + 1, k - 1, target - nums[i], path, res) path.pop()
<div>Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: 1. Each of the digits 1-9 must occur exactly once in each row. 2. Each of the digits 1-9 must occur exactly once in each column. 3. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. The '.' character indicates empty cells. Example 1:
<pre>Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
Explanation: The input board is shown above and the only valid solution is shown below:
</pre>
Constraints: * board.length == 9 * board[i].length == 9 * board[i][j] is a digit or '.'. * It is guaranteed that the input board has only one solution.</div>
题目大意:
日本游戏。需要保证每行每列每个9个方块的数是1-9里唯一。
Global dict解题思路(推荐):
DFS。利用DFS模板
解题步骤:
N/A
注意事项:
用三种全局性dict(row, col, box)来记录所有已填的数,方便dfs时候迅速判断是否合法。这是比算法II优胜的地方。Python中不存在list of set只能用list of dict: [collections.defaultdict(int) for _ in range(len(board))]
defsolveSudoku(self, board: List[List[str]]) -> None: row_dict = [collections.defaultdict(int) for _ inrange(len(board))] # remember col_dict = [collections.defaultdict(int) for _ inrange(len(board))] box_dict = [collections.defaultdict(int) for _ inrange(len(board))]
for i inrange(len(board)): for j inrange(len(board[0])): if board[i][j] != '.': self.add_to_dict(board, i, j, row_dict, col_dict, box_dict) # rememeber returnself.dfs(board, 0, 0, row_dict, col_dict, box_dict)
defis_valid(self, board, k, i, j, row_dict, col_dict, box_dict): if k in row_dict[i] or k in col_dict[j] or k in box_dict[i // 3 * 3 + j // 3]: # remember returnFalse returnTrue
defis_sudoku2(self, board, x, y): # row, # col, # square ifself.is_valid(board, x, 0, x, len(board[0]) - 1) andself.is_valid(board, 0, y, len(board) - 1, y) and \ self.is_valid(board, x // 3 * 3, y // 3 * 3, x // 3 * 3 + 2, y // 3 * 3 + 2): returnTrue else: returnFalse
defis_valid(self, board, start_x, start_y, end_x, end_y): num_set = set() for i inrange(start_x, end_x + 1): for j inrange(start_y, end_y + 1): val = board[i][j] if val == '.': continue ifint(val) in num_set: returnFalse num_set.add(int(val)) returnTrue
Given two nodes of a binary tree p and q, return their lowest common ancestor (LCA).
Each node will have a reference to its parent node. The definition for Node is below:
<pre>class Node {
public int val;
public Node left;
public Node right;
public Node parent;
}
</pre>
According to the definition of LCA on Wikipedia: "The lowest common ancestor of two nodes p and q in a tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)."
Example 1:
<pre>Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
</pre>
Example 2:
<pre>Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5 since a node can be a descendant of itself according to the LCA definition.
</pre>
The number of nodes in the tree is in the range [2, 10<sup>5</sup>].
-10<sup>9</sup> <= Node.val <= 10<sup>9</sup>
All Node.val are unique.
p != q
p and q exist in the tree.
</div>
题目大意:
求带父节点的树中的两个节点的LCA。节点值唯一,且两输入节点不同,且一定存在
解题思路:
N/A
解题步骤:
N/A
注意事项:
某一个节点的左右父节点存入set中,另一节点的每个父节点在set中找
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12
deflowestCommonAncestor(self, p: 'Node', q: 'Node') -> 'Node': parent_set = set() it = p while it: parent_set.add(it) it = it.parent it = q while it: if it in parent_set: return it it = it.parent returnNone