Skip to content

Commit 87dc8f8

Browse files
committed
20190701
1 parent 0554cec commit 87dc8f8

25 files changed

+48
-26
lines changed

Diff for: code/lc1027.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static int longestArithSeqLength(int[] A) {
1414
int[][] dp = new int[A.length][20000];
1515
for (int i = 0; i < A.length ; i++) {
1616
for (int j = 0; j < i ; j++) {
17-
int ind = A[i]-A[j]+10000;
17+
int ind = A[i]-A[j]+10000; //可能是负数,做一个偏移
1818
dp[i][ind] = dp[j][ind] + 1;
1919
res = Math.max(res, dp[i][ind]);
2020
}

Diff for: code/lc114.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Medium
66
* 分类:Tree, Depth-first Search
77
* 思路:就是节点间连接换一下,理清思路就行了,类似中序遍历
8-
* Tips:
8+
* Tips:lc426
99
*/
1010
public class lc114 {
1111
public class TreeNode {

Diff for: code/lc125.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* 分类:Two Pointers, String
77
* 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。
88
* Tips:记下另一种方法。第一种方法Bingo!
9-
* lc234, lc5
9+
* lc5, lc9, lc125, lc131, lc234, lc647
1010
*/
1111
public class lc125 {
1212
public static void main(String[] args) {

Diff for: code/lc127.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public int ladderLength(String beginWord, String endWord, List<String> wordList)
2929
curr_str[j] = k;
3030
if(String.valueOf(curr_str).equals(endWord)) return level;
3131
if(wordList.contains(String.valueOf(curr_str))){
32-
wordList.remove(String.valueOf(curr_str));
32+
wordList.remove(String.valueOf(curr_str)); //这要remove
3333
qu.add(String.valueOf(curr_str));
3434
}
3535
}

Diff for: code/lc131.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
* 难度:Medium
99
* 分类:Backtracking
1010
* 思路:典型回溯法,注意向res添加内容时要重新new一下
11-
* Tips:lc39
11+
* Tips: lc5, lc9, lc125, lc131, lc234, lc647
12+
* lc39
13+
* 判断是否为回文的方法:
14+
* 1. 从中心往两边扩充,中心可能是一个字符,也可能是两个字符
15+
* 2. dp,利用之前计算的结果,只判断边缘两个字符是否相等,从后往前dp
16+
* 3. 翻转了以后,判断两个串是否相等
1217
*/
1318
public class lc131 {
1419
public static void main(String[] args) {

Diff for: code/lc218.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public int compare(int[] o1, int[] o2) {
3939
int pre = 0;
4040
for (int i = 0; i < arr.length ; i++) {
4141
if(arr[i][1]>0){
42-
pr.add(-arr[i][1]);
42+
pr.add(-arr[i][1]); //默认是最小优先队列
4343
}else{
4444
pr.remove(arr[i][1]);
4545
}

Diff for: code/lc22.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:String, Backtracking
77
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
88
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
9+
* lc32, lc22, lc301
910
*/
1011
import java.util.ArrayList;
1112
import java.util.HashSet;

Diff for: code/lc227.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public int calculate(String s) {
2121
if(Character.isDigit(chs[i])){
2222
num = num * 10 + chs[i]-'0';
2323
}
24-
if( !Character.isDigit(chs[i]) || i==chs.length-1 ){ //便利到最后,即使不是符号,也要计算
24+
if( !Character.isDigit(chs[i]) || i==chs.length-1 ){ //遍历到最后,即使不是符号,也要计算
2525
if(sign=='+'){
2626
st.push(num);
2727
}

Diff for: code/lc234.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:LinkedList, Two Pointers
77
* 思路:反转一半就行了,避免了空间开销
88
* Tips:很好的题,考了 Two Pointers, 还考了链表反转
9+
* lc5, lc9, lc125, lc131, lc234, lc647
910
*/
1011
public class lc234 {
1112
public class ListNode {

Diff for: code/lc237.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* 题意:删除链表中的一个节点,给的是这个节点,不知道前边的节点
55
* 难度:Easy
66
* 分类:Linked List
7-
* 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,倒数第二个节点置空
7+
* 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,删除下一个节点
88
* Tips:
99
*/
1010
public class lc237 {
@@ -17,12 +17,7 @@ public class ListNode {
1717
}
1818
}
1919
public void deleteNode(ListNode node) {
20-
ListNode pre = new ListNode(-1);
21-
while(node.next!=null) {
22-
node.val = node.next.val;
23-
pre = node;
24-
node = node.next;
25-
}
26-
pre.next = null;
20+
node.val = node.next.val;
21+
node.next = node.next.next;
2722
}
2823
}

Diff for: code/lc239.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static int[] maxSlidingWindow(int[] nums, int k) {
2727
int cur = 0;
2828
Deque<Integer> dq = new ArrayDeque(); //队列里是递减的
2929
for (int i = 0; i < nums.length ; i++) {
30-
if( !dq.isEmpty() && dq.peekFirst()<=i-k)
30+
if( !dq.isEmpty() && dq.peekFirst()<=i-k) //窗口长度过长了,删掉头
3131
dq.removeFirst();
3232
while( !dq.isEmpty() && nums[dq.peekLast()]<=nums[i]){// removeLast 不是 First。 自己写的时候这写错了,如果是First的话,有些Case也能过
3333
dq.removeLast();

Diff for: code/lc240.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Medium
66
* 分类:Binary Search, Divide and Conquer
77
* 思路:两种方法,一种O(mlg(n)),遍历每一行,每行二分查找。另一种O(m+n),从右上角开始移动
8-
* Tips:
8+
* Tips:lc240, lc378
99
*/
1010
public class lc240 {
1111
public boolean searchMatrix(int[][] matrix, int target) {

Diff for: code/lc279.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Medium
66
* 分类:Math, Dynamic Programming, Breadth-first Search
77
* 思路:dp[i] = dp[i-j*j] +1
8-
* Tips:
8+
* Tips:lc322
99
*/
1010
import java.util.Arrays;
1111

Diff for: code/lc297.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private void buildString(TreeNode node, StringBuilder sb) {
4242
// Decodes your encoded data to tree.
4343
public TreeNode deserialize(String data) {
4444
Deque<String> nodes = new LinkedList<>();
45-
nodes.addAll(Arrays.asList(data.split(spliter)));
45+
nodes.addAll(Arrays.asList(data.split(spliter))); //split
4646
return buildTree(nodes);
4747
}
4848

Diff for: code/lc3.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 难度:Medium
66
* 分类:Hash Table, Two Pointers, String
77
* 算法:两个指针,记录没有重复字母的子串的首和尾
8+
* lc76
89
*/
910
import java.util.HashMap;
1011

Diff for: code/lc301.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:Depth-first Search, Breadth-first Search
77
* 思路:先计数),如果多的话,在前边的字符里删掉一个。反转字符串,计数(
88
* Tips:好难啊,里边很多细节需要注意。还有一种bfs的思路,挨个删字符,判断是否合规。
9+
* lc32, lc22, lc301
910
*/
1011
import java.util.ArrayList;
1112
import java.util.HashSet;

Diff for: code/lc309.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
* 难度:Medium
66
* 分类:Dynamic Programming
77
* 思路:状态DP,自己不会写。要分两种状态,手中有股票时最大收益,手中没股票时最大收益(包括冷冻期)。
8+
* buy[i] means before day i what is the maxProfit for any sequence end with buy.
89
* buy[i] = max( buy[i-1], sell[i-2]-price[i] )
910
* sell[i] = max( sell[i-1], buy[i-1]+price[i] )
10-
* 空间压缩以后时间是O(n),空间是O(1)
11+
* 压缩以后时间是O(n),空间是O(1)
1112
* Tips:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75931/Easiest-JAVA-solution-with-explanations
1213
* lc121, lc309, lc188, lc123, lc714
1314
*/
1415
public class lc309 {
1516
public int maxProfit(int[] prices) {
1617
if(prices.length==0) return 0;
17-
int b1 = -prices[0];
18+
int b1 = -prices[0]; //注意这里的初始化
1819
int s2=0, s1=0;
1920
int b = 0, s = 0;
2021
for (int i = 0; i < prices.length ; i++) {

Diff for: code/lc315.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* 难度:Hard
1010
* 分类:Divide and Conquer, Binary indexed Tree, Segment Tree, Binary Search Tree
1111
* 思路:两种思路,一种用二叉搜索树这类数据结构 https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST
12-
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将ndex进行排序
12+
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将index进行排序
1313
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation
1414
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索
1515
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code

Diff for: code/lc32.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 分类:Dynamic Programming, String
1010
* 思路:两种常规方法,一是dp,每个位置记录以该位置结尾的最长长度。另一种是用栈,把位置索引入栈。
1111
* Tips:想到了用dp,也想到了用数组记录位置结尾的解,但没有想好如何进行更新迭代计算。把位置索引入栈的方法很典型,关注一下。
12+
* lc32, lc22, lc301
1213
*/
1314
public class lc32 {
1415
public static void main(String[] args) {

Diff for: code/lc322.java

+15
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ public static int coinChange(int[] coins, int amount) {
3535
}
3636
return dp[amount-1]==Integer.MAX_VALUE ? -1 : dp[amount-1]; //没解返回-1
3737
}
38+
39+
public static int coinChange2(int[] coins, int amount) {
40+
int max = amount + 1;
41+
int[] dp = new int[amount + 1];
42+
Arrays.fill(dp, max);
43+
dp[0] = 0;
44+
for (int i = 1; i <= amount; i++) {
45+
for (int j = 0; j < coins.length; j++) {
46+
if (coins[j] <= i) {
47+
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
48+
}
49+
}
50+
}
51+
return dp[amount] > amount ? -1 : dp[amount];
52+
}
3853
}

Diff for: code/lc334.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* 题意:数组中是否存在递增的3个数
55
* 难度:Medium
66
* 分类:Array
7-
* 思路:思路很清奇,自己没想到,时间空间复杂度都是O(1)。其实和lc300 O(nlgn) 解法思路是一样的,只是固定了dp数组长度为两个数。
7+
* 思路:思路很清奇,自己没想到,时间复杂度O(N),空间是O(1)。其实和lc300 O(nlgn) 解法思路是一样的,只是固定了dp数组长度为两个数。
88
* Tips:lc300最长递增子序列
99
*/
1010
public class lc334 {

Diff for: code/lc378.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
1212
* 2是二分,二分的是val,看比这个val小的数是不是k
1313
* Tips:lc23方法很像
14-
* lc240
14+
* lc240, lc378
1515
*/
1616
public class lc378 {
1717
class Cell{

Diff for: code/lc5.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:String, Dynamic Programming
77
* Tips:从后往前遍历,保证后续dp时,子情况已计算出
88
* 还有一种思路是从中间往两边扩展,中间有两种情况,一种一个字符,一种两个字符
9+
* lc5, lc9, lc125, lc131, lc234, lc647
910
*/
1011
public class lc5 {
1112
public static void main(String[] args) {

Diff for: code/lc647.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Medium
66
* 分类:String, Dynamic Programming
77
* 思路:时间为N^2,用二维dp空间复杂度是N^2. 该题直接让判断是否回文,直接选择找中心字符,向两边拓展,空间为O(1)
8-
* Tips:
8+
* Tips:lc5, lc9, lc125, lc131, lc234, lc647
99
*/
1010
public class lc647 {
1111
public static void main(String[] args) {

Diff for: code/lc9.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 难度:Easy
66
* 分类:Math
77
* 思路:不转换字符串的思路就是把数字反转了以后,比较是否相等
8-
* Tips:
8+
* Tips:lc5, lc9, lc125, lc131, lc234, lc647
99
*/
1010
public class lc9 {
1111
public boolean isPalindrome(int x) {

0 commit comments

Comments
 (0)