分治
基础知识
分治也是一种编程思想,而不是具体的算法,因此这里介绍一些使用了分治思想的算法。
归并排序
题目解析
将有序数组转换为二叉搜索树
1.题目描述
2.解析思路及代码
分治的思想:将数组分为两部分,中间节点是根节点,根节点的左子树和右子树继续使用函数递归构建。
public TreeNode sortedArrayToBST(int[] nums) {
return helper(nums, 0, nums.length - 1);
}
public TreeNode helper(int[] nums, int l, int r) {
if (l > r) return null;
int mid = l + (r - l) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = helper(nums, l, mid - 1);
root.right = helper(nums, mid + 1, r);
return root;
}
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
def helper(l, r):
if l > r:
return None
mid = int(l + (r - l) / 2)
root = TreeNode(nums[mid])
root.left = helper(l, mid - 1)
root.right = helper(mid + 1, r)
return root
return helper(0, len(nums) - 1)
从中序与后序遍历序列构造二叉树
1.题目描述
2.解题思路及代码
可以根据后序遍历的定义得到后序遍历的最后一个节点是根节点,然后前面是右子树的结果(右子树的最后一个也是右子树的根节点),然后再往前面是左子树的结果。因此需要根据后序遍历的结果确定根节点,然后构造右子树,最后构造左子树。
class Solution {
private int[] postorder;
private int[] inorder;
private int post_idx;
private Map<Integer, Integer> idx_map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
this.postorder = postorder;
this.inorder = inorder;
this.post_idx = postorder.length - 1;
for (int i = 0; i < inorder.length; i ++ ) {
idx_map.put(inorder[i], i);
}
return helper(0, inorder.length - 1);
}
public TreeNode helper(int begin, int end) {
if (begin > end) return null;
TreeNode root = new TreeNode(postorder[post_idx]);
int index = idx_map.get(postorder[post_idx]);
post_idx -- ;
root.right = helper(index + 1, end);
root.left = helper(begin, index - 1);
return root;
}
}
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
def helper(begin, end):
if begin > end:
return None
val = postorder.pop()
root = TreeNode(val)
index = idx_map[val]
root.right = helper(index + 1, end)
root.left = helper(begin, index - 1)
return root
idx_map = {val:idx for idx, val in enumerate(inorder)}
return helper(0, len(inorder) - 1)
多数元素
1.题目描述
2.解题思路及代码
- 哈希表记录每个数出现的次数,找到出现次数大于等于 n / 2的结果
- 排序之后,出现在数组中间的数一定是最后的答案
- 分治:如果数a是数组nums的众数,那么也一定是数组nums分成两个数组之后的某个数组的众数
public int majorityElement(int[] nums) {
return majorityElement(nums, 0, nums.length - 1);
}
private int countInRange(int[] nums, int num, int begin, int end) {
int count = 0;
for (int i = begin; i <= end; i ++ ) {
if (nums[i] == num) count ++ ;
}
return count;
}
public int majorityElement(int[] nums, int begin, int end) {
if (begin == end) {
return nums[begin];
}
int mid = begin + (end - begin) / 2;
int left = majorityElement(nums, begin, mid);
int right = majorityElement(nums, mid + 1, end);
if (left == right) return left;
int leftCount = countInRange(nums, left, begin, end);
int rightCount = countInRange(nums, right, begin, end);
return leftCount > rightCount ? left : right;
}
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums.sort()
return nums[len(nums) // 2]
最大子数组和
1.题目描述
2.解题思路及代码
- 动态规划:dp[i] = max(dp[i - 1] + nums[i], nums[i])
- 分治:类似于线段树,这里不再讲解,详解可看链接中的题解
public int maxSubArray(int[] nums) {
if (nums.length == 0) return 0;
int[] res = new int[nums.length];
res[0] = nums[0];
int ans = nums[0];
for (int i = 1; i < nums.length; i ++ ) {
res[i] = Math.max(res[i - 1] + nums[i], nums[i]);
ans = Math.max(ans, res[i]);
}
return ans;
}
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
res = []
res.append(nums[0])
for i in nums[1:]:
res.append(max(res[-1] + i, i))
return max(res)
从前序与中序遍历序列构造二叉树
1.题目描述
2.解题思路及代码
思路:通过前序遍历可以确定第一个节点是根节点,然后是左子树,最后是右子树,而通过根节点和中序遍历可以把左子树和右子树确定出来,然后继续递归根据前序遍历的结果确定左子树的根节点和右子树的根节点。
class Solution {
private int[] preorder;
private int[] inorder;
private int pre_idx;
private Map<Integer, Integer> idx_map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.preorder = preorder;
this.inorder = inorder;
this.pre_idx = 0;
for (int i = 0; i < inorder.length; i ++ ) {
idx_map.put(inorder[i], i);
}
return helper(0, preorder.length - 1, 0, inorder.length - 1);
}
public TreeNode helper(int pre_st, int pre_ed, int in_st, int in_ed) {
if (pre_st > pre_ed) return null;
int value = preorder[pre_st];
TreeNode root = new TreeNode(value);
int index = idx_map.get(value);
int size_left = index - in_st;
root.left = helper(pre_st + 1, pre_st + size_left, in_st, index - 1);
root.right = helper(pre_st + size_left + 1, pre_ed, index + 1, in_ed);
return root;
}
}
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
def helper(pre_st, pre_ed, in_st, in_ed):
if pre_st > pre_ed:
return None
value = preorder[pre_st]
index = idx_map[value]
root = TreeNode(value)
size_left = index - in_st;
root.left = helper(pre_st + 1, pre_st + size_left, in_st, index - 1);
root.right = helper(pre_st + size_left + 1, pre_ed, index + 1, in_ed);
return root
idx_map = {val:idx for idx, val in enumerate(inorder)}
return helper(0, len(preorder) - 1, 0, len(inorder) - 1)
打卡内容
学习了动态规划相关题目的基本步骤,以及贪心算法的讲解,因为有部分题目可以用贪心也可以用动态规划,其中贪心实现很简单,因此能用贪心的就用贪心,动态规划有4个步骤:确定状态定义、状态转移方程、初始条件和边界情况、确定计算顺序。