转载:https://labuladong.gitee.io/algo/di-san-zha-24031/jing-dian–a94a0/fen-zhi-su-65a39/
void sort(int[] nums, int lo, int hi) {
int mid = (lo + hi) / 2;
/****** 分 ******/
// 对数组的两部分分别排序
sort(nums, lo, mid);
sort(nums, mid + 1, hi);
/****** 治 ******/
// 合并两个排好序的子数组
merge(nums, lo, mid, hi);
}
力扣第 241 题「为运算表达式设计优先级」来讲讲什么是分治算法 :https://leetcode.cn/problems/different-ways-to-add-parentheses/
// 计算所有加括号的结果
List<Integer> diffWaysToCompute(String input);
// 定义:计算算式 input 所有可能的运算结果
List<Integer> diffWaysToCompute(String input);
List<Integer> diffWaysToCompute("(1 + 2 * 3) - (4 * 5)") {
List<Integer> res = new LinkedList<>();
/****** 分 ******/
List<Integer> left = diffWaysToCompute("1 + 2 * 3");
List<Integer> right = diffWaysToCompute("4 * 5");
/****** 治 ******/
for (int a : left)
for (int b : right)
res.add(a - b);
return res;
}
List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new LinkedList<>();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
// 扫描算式 input 中的运算符
if (c == '-' || c == '*' || c == '+') {
/****** 分 ******/
// 以运算符为中心,分割成两个字符串,分别递归计算
List<Integer>
left = diffWaysToCompute(input.substring(0, i));
List<Integer>
right = diffWaysToCompute(input.substring(i + 1));
/****** 治 ******/
// 通过子问题的结果,合成原问题的结果
for (int a : left)
for (int b : right)
if (c == '+')
res.add(a + b);
else if (c == '-')
res.add(a - b);
else if (c == '*')
res.add(a * b);
}
}
// base case
// 如果 res 为空,说明算式是一个数字,没有运算符
if (res.isEmpty()) {
res.add(Integer.parseInt(input));
}
return res;
}
// base case
// 如果 res 为空,说明算式是一个数字,没有运算符
if (res.isEmpty()) {
res.add(Integer.parseInt(input));
}
// 备忘录
HashMap<String, List<Integer>> memo = new HashMap<>();
List<Integer> diffWaysToCompute(String input) {
// 避免重复计算
if (memo.containsKey(input)) {
return memo.get(input);
}
/****** 其他都不变 ******/
List<Integer> res = new LinkedList<>();
for (int i = 0; i < input.length(); i++) {
// ...
}
if (res.isEmpty()) {
res.add(Integer.parseInt(input));
}
/***********************/
// 将结果添加进备忘录
memo.put(input, res);
return res;
}
https://labuladong.gitee.io/algo/di-san-zha-24031/jing-dian–a94a0/fen-zhi-su-65a39/