-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
441 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] 前缀和的解法还需要再思考思考! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/diguage/algorithm/leetcode/_0112_PathSum_2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/diguage/algorithm/leetcode/_0113_PathSumII_2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/diguage/algorithm/leetcode/_0113_PathSumII_21.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/com/diguage/algorithm/leetcode/_0437_PathSumIII_2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.