Skip to content

Commit

Permalink
再次完成 112,113,437
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Jun 20, 2024
1 parent 53b6457 commit 7334cb0
Show file tree
Hide file tree
Showing 27 changed files with 441 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3066,8 +3066,8 @@
|{leetcode_base_url}/path-sum-iii/[Path Sum III]
|{source_base_url}/_0437_PathSumIII.java[Java]
|{doc_base_url}/0437-path-sum-iii.adoc[Note]
|Easy
|
|Medium
|前缀和解法还需要多思考!

|438
|{leetcode_base_url}/find-all-anagrams-in-a-string/[Find All Anagrams in a String]
Expand Down
3 changes: 2 additions & 1 deletion docs/0000-00-note.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
= 刷题随想

. xref:0215-kth-largest-element-in-an-array.adoc[] 没想到快速排序的分区算法,竟然可以用于做快速选择?!神奇… 可惜的是,这题目以前做过,现在都给忘完了…
. xref:0215-kth-largest-element-in-an-array.adoc[215. Kth Largest Element in an Array] 没想到快速排序的分区算法,竟然可以用于做快速选择?!神奇… 可惜的是,这题目以前做过,现在都给忘完了…
. xref:0437-path-sum-iii.adoc[437. Path Sum III] 前缀和的解法还需要再思考思考!
12 changes: 12 additions & 0 deletions docs/0112-path-sum.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[#0112-path-sum]
= 112. Path Sum

https://leetcode.com/problems/path-sum/[LeetCode - Path Sum]
Expand Down Expand Up @@ -38,3 +39,14 @@ return true, as there exist a root-to-leaf path `5->4->11->2` which sum is 22.
include::{sourcedir}/_0112_PathSum.java[]
----

[{java_src_attr}]
----
include::{sourcedir}/_0112_PathSum_2.java[]
----

很简单的一道题。本来是想找另外一道题(见 <<0112-additional-question>>),结果找到了这道题。

[#0112-additional-question]
== 思考题

给定一颗二叉树和一个整数 sum,求累加和为 sum 的最长路径长度。路径是指从某个节点往下,每次最多选择一个孩子节点或者不选所形成的节点链。
42 changes: 42 additions & 0 deletions docs/0113-path-sum-ii.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[#0113-path-sum-ii]
= 113. Path Sum II

https://leetcode.com/problems/path-sum-ii/[LeetCode - Path Sum II]
Expand Down Expand Up @@ -47,3 +48,44 @@ Return:
include::{sourcedir}/_0113_PathSumII.java[]
----

== 未优化版

[{java_src_attr}]
----
include::{sourcedir}/_0113_PathSumII_2.java[]
----

== 优化版

[{java_src_attr}]
----
include::{sourcedir}/_0113_PathSumII_21.java[]
----

TIP: 非常典型的回溯问题!


image::images/0113-01.png[]

image::images/0113-02.png[]

image::images/0113-03.png[]

image::images/0113-04.png[]

image::images/0113-05.png[]

image::images/0113-06.png[]

image::images/0113-07.png[]

image::images/0113-08.png[]

image::images/0113-09.png[]

image::images/0113-10.png[]

image::images/0113-11.png[]

image::images/0113-12.png[]

26 changes: 26 additions & 0 deletions docs/0437-path-sum-iii.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[#0437-path-sum-iii]
= 437. Path Sum III

https://leetcode.com/problems/path-sum-iii/[LeetCode - Path Sum III]
Expand All @@ -8,6 +9,8 @@ Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

image::images/0437-00.jpg[]

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

.Example:
Expand Down Expand Up @@ -69,3 +72,26 @@ Return 3. The paths that sum to 8 are:
include::{sourcedir}/_0437_PathSumIII.java[]
----
.错误提交
[{java_src_attr}]
----
include::{sourcedir}/_0437_PathSumIII_2.java[]
----
[{java_src_attr}]
----
include::{sourcedir}/_0437_PathSumIII_21.java[]
----
== 前缀和解法
[{java_src_attr}]
----
include::{sourcedir}/_0437_PathSumIII_22.java[]
----
== 参考资料
. https://leetcode.cn/problems/path-sum-iii/solutions/596361/dui-qian-zhui-he-jie-fa-de-yi-dian-jie-s-dey6/[437. 路径总和 III / 对前缀和解法的一点解释^]
. https://leetcode.cn/problems/path-sum-iii/solutions/100992/qian-zhui-he-di-gui-hui-su-by-shi-huo-de-xia-tian/[437. 路径总和 III / 前缀和,递归,回溯^]
Binary file added docs/images/0113-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0113-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0437-00.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* https://leetcode.com/problems/path-sum/[Path Sum - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @author D瓜哥 · https://www.diguage.com
* @since 2020-02-07 21:53
*/
public class _0112_PathSum {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/diguage/algorithm/leetcode/_0112_PathSum_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.diguage.algorithm.leetcode;

import com.diguage.algorithm.util.TreeNode;

import java.util.Objects;

import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
import static java.util.Arrays.asList;

/**
* = 112. Path Sum
*
* https://leetcode.com/problems/path-sum/[Path Sum - LeetCode]
*
* @author D瓜哥 · https://www.diguage.com
* @since 2024-06-20 15:15:50
*/
public class _0112_PathSum_2 {

public boolean hasPathSum(TreeNode root, int sum) {
if (Objects.isNull(root)) {
return false;
}
if (root.val == sum && root.left == null && root.right == null) {
return true;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}

public static void main(String[] args) {
_0112_PathSum_2 solution = new _0112_PathSum_2();
boolean r1 = solution.hasPathSum(buildTree(asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, null, null, 1)), 22);
System.out.println(r1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* https://leetcode.com/problems/path-sum-ii/[Path Sum II - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @author D瓜哥 · https://www.diguage.com
* @since 2020-02-07 22:31
*/
public class _0113_PathSumII {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.diguage.algorithm.leetcode;

import com.diguage.algorithm.util.TreeNode;

import java.util.*;

import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
import static java.util.Arrays.asList;

/**
* = 113. Path Sum II
*
* https://leetcode.com/problems/path-sum-ii/[Path Sum II - LeetCode]
*
* @author D瓜哥 · https://www.diguage.com
* @since 2020-02-07 22:31
*/
public class _0113_PathSumII_2 {

/**
* 原始解法
*/
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if (root == null) {
return Collections.emptyList();
}
List<List<Integer>> result = new ArrayList<>();
pathSum(root, sum, result, new ArrayList<>());
return result;
}

private void pathSum(TreeNode root, int sum, List<List<Integer>> result, List<Integer> path) {
if (root == null) {
return;
}
int nextSum = sum - root.val;
if (nextSum == 0 && root.left == null && root.right == null) {
path.add(root.val);
result.add(path);
} else {
// 这里每次都需要创建 List 对象,可以优化一下
ArrayList<Integer> lp = new ArrayList<>(path);
lp.add(root.val);
pathSum(root.left, nextSum, result, lp);

// 这里每次都需要创建 List 对象,可以优化一下
ArrayList<Integer> rp = new ArrayList<>(path);
rp.add(root.val);
pathSum(root.right, nextSum, result, rp);
}
}

public static void main(String[] args) {
_0113_PathSumII_2 solution = new _0113_PathSumII_2();
List<List<Integer>> r1 = solution.pathSum(buildTree(asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, null, 5, 1)), 22);
System.out.println(r1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.diguage.algorithm.leetcode;

import com.diguage.algorithm.util.TreeNode;

import java.util.*;

import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
import static java.util.Arrays.asList;

/**
* = 113. Path Sum II
*
* https://leetcode.com/problems/path-sum-ii/[Path Sum II - LeetCode]
*
* @author D瓜哥 · https://www.diguage.com
* @since 2020-02-07 22:31
*/
public class _0113_PathSumII_21 {

/**
* 原始解法
*/
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if (root == null) {
return Collections.emptyList();
}
List<List<Integer>> result = new ArrayList<>();
Deque<Integer> path = new LinkedList<>();
pathSum(root, sum, result, path);
return result;
}

private void pathSum(TreeNode root, int sum, List<List<Integer>> result, Deque<Integer> path) {
if (root == null) {
return;
}
int nextSum = sum - root.val;
// 用前添加
path.addLast(root.val);
if (nextSum == 0 && root.left == null && root.right == null) {
result.add(new ArrayList<>(path));
}
pathSum(root.left, nextSum, result, path);

pathSum(root.right, nextSum, result, path);

// 用完删除,这不就是回溯吗?
path.removeLast();
}

public static void main(String[] args) {
_0113_PathSumII_21 solution = new _0113_PathSumII_21();
List<List<Integer>> r1 = solution.pathSum(buildTree(asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, null, 5, 1)), 22);
System.out.println(r1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* https://leetcode.com/problems/path-sum-iii/[Path Sum III - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @author D瓜哥 · https://www.diguage.com
* @since 2020-01-28 23:06
*/
public class _0437_PathSumIII {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.diguage.algorithm.leetcode;

import com.diguage.algorithm.util.TreeNode;

import java.util.Arrays;
import java.util.Objects;

import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;

/**
* = 437. Path Sum III
*
* https://leetcode.com/problems/path-sum-iii/[Path Sum III - LeetCode]
*
* @author D瓜哥 · https://www.diguage.com
* @since 2020-01-28 23:06
*/
public class _0437_PathSumIII_2 {
/**
* Runtime: 23 ms, faster than 10.40% of Java online submissions for Path Sum III.
* Memory Usage: 39.1 MB, less than 90.91% of Java online submissions for Path Sum III.
*
* Copy from: https://leetcode-cn.com/problems/path-sum-iii/solution/leetcode-437-path-sum-iii-by-li-xin-lei/[LeetCode 437 Path Sum III - 路径总和 III - 力扣(LeetCode)]
*/
// public int pathSum(TreeNode root, int sum) {
// if (root == null) {
// return 0;
// }
// int nextSum = sum - root.val;
// if (nextSum == 0) {
// return 1 + pathSum(root.left, sum) + pathSum(root.right, sum);
// } else {
// return pathSum(root.left, sum) + pathSum(root.left, nextSum) +
// pathSum(root.right, sum) + pathSum(root.right, nextSum);
// }
// }
public int pathSum(TreeNode root, int sum) {
int result = 0;
if (root == null) {
return result;
}
if (sum == root.val) {
result++;
}
// TODO 哪里错误?
result += pathSum(root.left, sum - root.val);
result += pathSum(root.right, sum - root.val);
result += pathSum(root.left, sum);
result += pathSum(root.right, sum);
return result;
}

// public int pathSum(TreeNode root, long targetSum) {
// if (root == null) {
// return 0;
// }
//
// int ret = rootSum(root, targetSum);
// ret += pathSum(root.left, targetSum);
// ret += pathSum(root.right, targetSum);
// return ret;
// }
//
// public int rootSum(TreeNode root, long targetSum) {
// int ret = 0;
// if (root == null) {return 0;}
// int val = root.val;
// if (val == targetSum) {
// ret++;
// }
// ret += rootSum(root.left, targetSum - val);
// ret += rootSum(root.right, targetSum - val);
// return ret;
// }


public static void main(String[] args) {
_0437_PathSumIII_2 solution = new _0437_PathSumIII_2();
int r1 = solution.pathSum(buildTree(Arrays.asList(10, 5, -3, 3, 2, null, 11, 3, -2, null, 1)), 8);
System.out.println((r1 == 3) + " : " + r1);
}
}
Loading

0 comments on commit 7334cb0

Please sign in to comment.