forked from mJackie/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 11
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
3 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package code; | ||
/* | ||
* 301. Remove Invalid Parentheses | ||
* 题意:删掉不匹配的括号 | ||
* 难度:Hard | ||
* 分类:Depth-first Search, Breadth-first Search | ||
* 思路:先计数),如果多的话,在前边的字符里删掉一个。反转字符串,计数( | ||
* Tips:好难啊,里边很多细节需要注意。还有一种bfs的思路,挨个删字符,判断是否合规。 | ||
*/ | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
public class lc301 { | ||
public static void main(String[] args) { | ||
System.out.println(removeInvalidParentheses("()())()").toString()); | ||
} | ||
public static List<String> removeInvalidParentheses(String s) { | ||
HashSet<String> res = new HashSet(); | ||
StringBuilder sb = new StringBuilder(); | ||
helper(s, res, 0, 0, new char[]{'(',')'}); | ||
return new ArrayList<>(res); | ||
} | ||
public static void helper(String s, HashSet<String> res, int pos, int pos2, char[] chs){ | ||
int count = 0; | ||
char[] str_arr = s.toCharArray(); | ||
for (int i = pos; i < s.length() ; i++) { | ||
if( str_arr[i]==chs[0] ) | ||
count++; | ||
else if( str_arr[i]==chs[1] ) | ||
count--; | ||
if (count < 0) { //需要删除一个 ')' | ||
for (int j = pos2; j <= i; j++) { // pos2 避免重复的情况又算一次 | ||
if (str_arr[j] == chs[1] && j == 0) | ||
helper(s.substring(0,j)+s.substring(j+1), res, i, j,chs); | ||
if (str_arr[j] == chs[1] && j > 0 && str_arr[j - 1] != chs[1]) | ||
helper(s.substring(0,j)+s.substring(j+1), res, i, j,chs); //把截取后的s传到递归函数里, i不用+1,因为已经删掉一个字符了 | ||
} | ||
return; | ||
} | ||
} | ||
s = new StringBuilder(s).reverse().toString(); | ||
if(chs[0]=='(') | ||
helper(s, res, 0, 0, new char[]{')','('}); //反转 | ||
else | ||
res.add(s); | ||
} | ||
} |
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,77 @@ | ||
package code; | ||
|
||
import java.util.HashMap; | ||
|
||
/* | ||
* 437. Path Sum III | ||
* 题意:树中有几条节点和为sum的路径 | ||
* 难度:Easy | ||
* 分类:Tree | ||
* 思路:两种方法,一种类似数组中连续和为k的解法,hashmap. 另一种方法是递归 | ||
* Tips:自己写的自顶向下的 dfs 会出现重复情况,父节点算一次,父节点的父节点也调用它算了一次,注意这种情况。应对方法是写两个函数,在不同的地方调用。 | ||
* 递归的时候用减是否==0的方式,而不是+==sum的方式 | ||
* 和lc560有共同的思想,每个节点只需遍历一遍就可以了 | ||
* 虽然是Easy题,做好也不简单 | ||
*/ | ||
public class lc437 { | ||
public static class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode(int x) { | ||
val = x; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
TreeNode node10 = new TreeNode(10); | ||
TreeNode node5 = new TreeNode(5); | ||
TreeNode node3 = new TreeNode(3); | ||
TreeNode node2 = new TreeNode(2); | ||
TreeNode node1 = new TreeNode(1); | ||
TreeNode noden3 = new TreeNode(-3); | ||
TreeNode node11 = new TreeNode(11); | ||
node10.left = node5; | ||
node10.right = noden3; | ||
node5.left = node3; | ||
node5.right = node2; | ||
node2.right = node1; | ||
noden3.right = node11; | ||
System.out.println(pathSum2(node10, 8)); | ||
} | ||
|
||
|
||
public static int pathSum(TreeNode root, int sum) { //该节点作为起点 | ||
if (root == null) return 0; | ||
return dfs(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); | ||
} | ||
|
||
public static int dfs(TreeNode root, int sum) { //一条路径向下走 | ||
if (root == null) | ||
return 0; | ||
if (root.val == sum) | ||
return 1 + dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val); | ||
return dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val); | ||
} | ||
|
||
|
||
public static int pathSum2(TreeNode root, int sum) {//类似连续和为k的解法 | ||
if (root == null) return 0; | ||
HashMap<Integer, Integer> hs = new HashMap<>(); | ||
hs.put(0, 1); | ||
return helper(root, sum, hs, 0); | ||
} | ||
public static int helper(TreeNode root, int sum, HashMap<Integer, Integer> hs, int cur_sum) { | ||
if (root == null) | ||
return 0; | ||
cur_sum += root.val; | ||
int res = hs.getOrDefault(cur_sum - sum, 0); //该节点作为尾节点的解的个数 | ||
hs.put(cur_sum, hs.getOrDefault(cur_sum, 0)+1); //注意与上一句顺序 | ||
res += helper(root.left, sum, hs, cur_sum); //加上孩子节点的解 | ||
res += helper(root.right, sum, hs, cur_sum); | ||
hs.put(cur_sum, hs.get(cur_sum)-1); | ||
return res; | ||
} | ||
} | ||
|
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,25 @@ | ||
package code; | ||
|
||
import java.util.HashMap; | ||
|
||
/* | ||
* 560. Subarray Sum Equals K | ||
* 题意:子数组的和等于k的个数 | ||
* 难度:Medium | ||
* 分类:Array, Hash Table | ||
* 思路:求出累加和存在hashmap中,如果当前hashmap中存在sum-k,那么就是一个解 | ||
* Tips:经典思路,记一下。lc437有类似思想。 | ||
*/ | ||
public class lc560 { | ||
public int subarraySum(int[] nums, int k) { | ||
HashMap<Integer, Integer> hs = new HashMap(); | ||
hs.put(0,1); | ||
int res = 0; | ||
for (int sum = 0, i = 0; i < nums.length ; i++) { | ||
sum += nums[i]; | ||
res += hs.getOrDefault(sum-k,0); // sum1+sum2 = k ,说明sum2-sum1那一段是一个解 | ||
hs.put(sum, hs.getOrDefault(sum,0)+1); | ||
} | ||
return res; | ||
} | ||
} |