Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ca0257

Browse files
committed
20190715
1 parent abfdac6 commit 4ca0257

19 files changed

+193
-46
lines changed

‎code/lc102.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public List<List<Integer>> levelOrder(TreeNode root) {
2323
if(root==null)
2424
return res;
2525
qu.add(root);
26-
while(!qu.isEmpty()){
26+
while(!qu.isEmpty()){//两个while
2727
int size = qu.size();
2828
List<Integer> temp = new ArrayList<>();
2929
while(size>0){

‎code/lc113.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ public class TreeNode {
2020
}
2121
}
2222
public List<List<Integer>> pathSum(TreeNode root, int sum) {
23-
List<List<Integer>> res = new ArrayList<>();
24-
helper(res, root, sum, 0, new ArrayList<>());
23+
List<List<Integer>> res = new ArrayList();
24+
helper(res, new ArrayList(), root, sum);
2525
return res;
2626
}
27-
public void helper(List<List<Integer>> res, TreeNoderoot, intsum, int curr, List<Integer> curr_ls) {
27+
public void helper(List<List<Integer>> res, List<Integer> cur, TreeNoderoot, int sum){
2828
if(root==null) return;
29-
curr_ls.add(root.val);
30-
if(curr+root.val==sum && root.left==null && root.right==null) { //到叶子节点
31-
res.add(new ArrayList<>(curr_ls));
32-
curr_ls.remove(curr_ls.size()-1);
33-
return;
29+
cur.add(root.val);
30+
if(root.left==null&&root.right==null&&root.val==sum){ //到叶子节点
31+
res.add(new ArrayList(cur));
32+
}else{
33+
helper(res, cur, root.left, sum-root.val);
34+
helper(res, cur, root.right, sum-root.val);
3435
}
35-
helper(res, root.left, sum, curr+root.val, curr_ls);
36-
helper(res, root.right, sum, curr+root.val, curr_ls);
37-
curr_ls.remove(curr_ls.size()-1);
36+
cur.remove(cur.size()-1);
37+
return;
3838
}
3939
}

‎code/lc123.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package code;
22
/*
3-
* 122. Best Time to Buy and Sell Stock III
3+
* 123. Best Time to Buy and Sell Stock III
44
* 题意:买卖股票最大利润,只能买卖2次
55
* 难度:Hard
66
* 分类:Array, Dynamic Programming

‎code/lc131.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* 思路:典型回溯法,注意向res添加内容时要重新new一下
1111
* Tips: lc5, lc9, lc125, lc131, lc234, lc647
1212
* lc39
13+
* lc132
1314
* 判断是否为回文的方法:
1415
* 1. 从中心往两边扩充,中心可能是一个字符,也可能是两个字符
1516
* 2. dp,利用之前计算的结果,只判断边缘两个字符是否相等,从后往前dp

‎code/lc132.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package code;
2+
import java.util.Arrays;
3+
4+
/*
5+
* 132. Palindrome Partitioning II
6+
* 题意:最少切几刀,回文串
7+
* 难度:Hard
8+
* 分类:Dynamic Programming
9+
* 思路:和lc300思路很像,只是多了判断。dp[i]表示从0到i这一段切法的最小值,每次遍历前边的结果,看能否接上。
10+
* 可以把判断回文和dp两个合并,因为用dp的形式判断回文
11+
* Tips:lc300
12+
* bingo!
13+
*/
14+
15+
public class lc132 {
16+
public int minCut(String s) {
17+
int[] dp = new int[s.length()+1];
18+
Arrays.fill(dp, s.length()); //fill最大值
19+
dp[0] = 0;
20+
for(int i=1; i<dp.length; i++){
21+
for(int j=0; j<=i; j++){ //注意<=
22+
if(dp[j]!=s.length() && isPalindrome(s.substring(j,i)))
23+
dp[i] = Math.min(dp[i], dp[j]+1);
24+
}
25+
}
26+
return dp[s.length()]-1; //要-1 最后一刀是在末尾
27+
}
28+
29+
public Boolean isPalindrome(String str){
30+
if(new StringBuilder(str).reverse().toString().equals(str)) return true; //reverse一下
31+
else return false;
32+
}
33+
34+
public int minCut2(String s) {
35+
char[] c = s.toCharArray();
36+
int n = c.length;
37+
int[] cut = new int[n];
38+
boolean[][] pal = new boolean[n][n];
39+
40+
for(int i = 0; i < n; i++) {
41+
int min = i;
42+
for(int j = 0; j <= i; j++) {
43+
if(c[j] == c[i] && (j + 1 > i - 1 || pal[j + 1][i - 1])) {
44+
pal[j][i] = true;
45+
min = j == 0 ? 0 : Math.min(min, cut[j - 1] + 1);
46+
}
47+
}
48+
cut[i] = min;
49+
}
50+
return cut[n - 1];
51+
}
52+
}

‎code/lc139.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
* 分类:Dynamic Programming
77
* 思路:动态规划
88
* Tips:巧妙的方法,防止了复杂的操作,通过遍历之前计算出来的结果
9+
* 递归的方法本质和dp是一样的,记住用备忘录算法,把之前的结果记下来
910
* lc140
1011
*/
12+
import java.util.HashMap;
1113
import java.util.List;
1214

1315
public class lc139 {
@@ -22,4 +24,16 @@ public boolean wordBreak(String s, List<String> wordDict) {
2224
}
2325
return dp[s.length()];
2426
}
27+
28+
HashMap<String, Boolean> hm = new HashMap();
29+
public boolean wordBreak2(String s, List<String> wordDict) {
30+
if(hm.containsKey(s)) return hm.get(s);
31+
if(s.length() == 0) return true;
32+
Boolean flag = false;
33+
for(String word:wordDict){
34+
if(s.startsWith(word)) flag = flag||wordBreak(s.substring(word.length()), wordDict); //注意函数 startsWith
35+
}
36+
hm.put(s, flag);
37+
return flag;
38+
}
2539
}

‎code/lc141.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ public class ListNode {
1414
ListNode(int x) { val = x; }
1515
}
1616
public boolean hasCycle(ListNode head) {
17-
if(head==null)
18-
return false;
19-
ListNode faster = head;
17+
if(head==null||head.next==null) return false;
2018
ListNode slow = head;
21-
while( faster.next!=null && faster.next.next!=null){ //注意判断条件,slow一定不等于null,不用判断了
19+
ListNode fast = head.next;
20+
while(fast!=null&&fast.next!=null){ //注意判断条件,slow一定不等于null,不用判断了
2221
slow = slow.next;
23-
faster = faster.next.next;
24-
if(slow==faster)
25-
return true;
22+
fast = fast.next.next;
23+
if(fast==slow) return true;
2624
}
2725
return false;
2826
}

‎code/lc142.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,23 @@ public ListNode detectCycle(ListNode head) {
3232
}
3333
return null;
3434
}
35+
36+
public ListNode detectCycle2(ListNode head) {
37+
if(head==null||head.next==null) return null;
38+
ListNode slow = head;
39+
ListNode fast = head.next;
40+
while(fast!=null&&fast.next!=null){
41+
slow = slow.next;
42+
fast = fast.next.next;
43+
if(slow==fast){
44+
slow = slow.next;
45+
while(head!=slow){
46+
head = head.next;
47+
slow = slow.next;
48+
}
49+
return slow;
50+
}
51+
}
52+
return null;
53+
}
3554
}

‎code/lc148.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ListNode sortList(ListNode head) {
2525
if( head==null || head.next == null ){
2626
return head;
2727
}
28-
ListNode slow = head;
28+
ListNode slow = head;//记一下
2929
ListNode fast = head.next;
3030
while( fast!=null && fast.next!=null ){ //把链表分成两半
3131
slow = slow.next;

‎code/lc207.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static boolean canFinish(int numCourses, int[][] prerequisites) {
2323
for (int i = 0; i < prerequisites.length ; i++) {
2424
int node1 = prerequisites[i][0];
2525
int node2 = prerequisites[i][1];
26-
graph[node2][node1] = 1;
26+
graph[node2][node1] = 1;//存下邻接矩阵,方便后续查找是否有边
2727
indegree[node1]++; //存下入度,入度为0时,表示该课程可以修
2828
}
2929
Stack<Integer> st = new Stack();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /