提交 87dc8f81 编写于 作者: L liu13

20190701

上级 0554cec6
...@@ -14,7 +14,7 @@ public class lc1027 { ...@@ -14,7 +14,7 @@ public class lc1027 {
int[][] dp = new int[A.length][20000]; int[][] dp = new int[A.length][20000];
for (int i = 0; i < A.length ; i++) { for (int i = 0; i < A.length ; i++) {
for (int j = 0; j < i ; j++) { for (int j = 0; j < i ; j++) {
int ind = A[i]-A[j]+10000; int ind = A[i]-A[j]+10000; //可能是负数,做一个偏移
dp[i][ind] = dp[j][ind] + 1; dp[i][ind] = dp[j][ind] + 1;
res = Math.max(res, dp[i][ind]); res = Math.max(res, dp[i][ind]);
} }
......
...@@ -5,7 +5,7 @@ package code; ...@@ -5,7 +5,7 @@ package code;
* 难度:Medium * 难度:Medium
* 分类:Tree, Depth-first Search * 分类:Tree, Depth-first Search
* 思路:就是节点间连接换一下,理清思路就行了,类似中序遍历 * 思路:就是节点间连接换一下,理清思路就行了,类似中序遍历
* Tips: * Tips:lc426
*/ */
public class lc114 { public class lc114 {
public class TreeNode { public class TreeNode {
......
...@@ -6,7 +6,7 @@ package code; ...@@ -6,7 +6,7 @@ package code;
* 分类:Two Pointers, String * 分类:Two Pointers, String
* 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。 * 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。
* Tips:记下另一种方法。第一种方法Bingo! * Tips:记下另一种方法。第一种方法Bingo!
* lc234, lc5 * lc5, lc9, lc125, lc131, lc234, lc647
*/ */
public class lc125 { public class lc125 {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -29,7 +29,7 @@ public class lc127 { ...@@ -29,7 +29,7 @@ public class lc127 {
curr_str[j] = k; curr_str[j] = k;
if(String.valueOf(curr_str).equals(endWord)) return level; if(String.valueOf(curr_str).equals(endWord)) return level;
if(wordList.contains(String.valueOf(curr_str))){ if(wordList.contains(String.valueOf(curr_str))){
wordList.remove(String.valueOf(curr_str)); wordList.remove(String.valueOf(curr_str)); //这要remove
qu.add(String.valueOf(curr_str)); qu.add(String.valueOf(curr_str));
} }
} }
......
...@@ -8,7 +8,12 @@ import java.util.List; ...@@ -8,7 +8,12 @@ import java.util.List;
* 难度:Medium * 难度:Medium
* 分类:Backtracking * 分类:Backtracking
* 思路:典型回溯法,注意向res添加内容时要重新new一下 * 思路:典型回溯法,注意向res添加内容时要重新new一下
* Tips:lc39 * Tips: lc5, lc9, lc125, lc131, lc234, lc647
* lc39
* 判断是否为回文的方法:
* 1. 从中心往两边扩充,中心可能是一个字符,也可能是两个字符
* 2. dp,利用之前计算的结果,只判断边缘两个字符是否相等,从后往前dp
* 3. 翻转了以后,判断两个串是否相等
*/ */
public class lc131 { public class lc131 {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -39,7 +39,7 @@ public class lc218 { ...@@ -39,7 +39,7 @@ public class lc218 {
int pre = 0; int pre = 0;
for (int i = 0; i < arr.length ; i++) { for (int i = 0; i < arr.length ; i++) {
if(arr[i][1]>0){ if(arr[i][1]>0){
pr.add(-arr[i][1]); pr.add(-arr[i][1]); //默认是最小优先队列
}else{ }else{
pr.remove(arr[i][1]); pr.remove(arr[i][1]);
} }
......
...@@ -6,6 +6,7 @@ package code; ...@@ -6,6 +6,7 @@ package code;
* 分类:String, Backtracking * 分类:String, Backtracking
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回 * 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案 * 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
* lc32, lc22, lc301
*/ */
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
......
...@@ -21,7 +21,7 @@ public class lc227 { ...@@ -21,7 +21,7 @@ public class lc227 {
if(Character.isDigit(chs[i])){ if(Character.isDigit(chs[i])){
num = num * 10 + chs[i]-'0'; num = num * 10 + chs[i]-'0';
} }
if( !Character.isDigit(chs[i]) || i==chs.length-1 ){ //便利到最后,即使不是符号,也要计算 if( !Character.isDigit(chs[i]) || i==chs.length-1 ){ //遍历到最后,即使不是符号,也要计算
if(sign=='+'){ if(sign=='+'){
st.push(num); st.push(num);
} }
......
...@@ -6,6 +6,7 @@ package code; ...@@ -6,6 +6,7 @@ package code;
* 分类:LinkedList, Two Pointers * 分类:LinkedList, Two Pointers
* 思路:反转一半就行了,避免了空间开销 * 思路:反转一半就行了,避免了空间开销
* Tips:很好的题,考了 Two Pointers, 还考了链表反转 * Tips:很好的题,考了 Two Pointers, 还考了链表反转
* lc5, lc9, lc125, lc131, lc234, lc647
*/ */
public class lc234 { public class lc234 {
public class ListNode { public class ListNode {
......
...@@ -4,7 +4,7 @@ package code; ...@@ -4,7 +4,7 @@ package code;
* 题意:删除链表中的一个节点,给的是这个节点,不知道前边的节点 * 题意:删除链表中的一个节点,给的是这个节点,不知道前边的节点
* 难度:Easy * 难度:Easy
* 分类:Linked List * 分类:Linked List
* 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,倒数第二个节点置空 * 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,删除下一个节点
* Tips: * Tips:
*/ */
public class lc237 { public class lc237 {
...@@ -17,12 +17,7 @@ public class lc237 { ...@@ -17,12 +17,7 @@ public class lc237 {
} }
} }
public void deleteNode(ListNode node) { public void deleteNode(ListNode node) {
ListNode pre = new ListNode(-1); node.val = node.next.val;
while(node.next!=null) { node.next = node.next.next;
node.val = node.next.val;
pre = node;
node = node.next;
}
pre.next = null;
} }
} }
...@@ -27,7 +27,7 @@ public class lc239 { ...@@ -27,7 +27,7 @@ public class lc239 {
int cur = 0; int cur = 0;
Deque<Integer> dq = new ArrayDeque(); //队列里是递减的 Deque<Integer> dq = new ArrayDeque(); //队列里是递减的
for (int i = 0; i < nums.length ; i++) { for (int i = 0; i < nums.length ; i++) {
if( !dq.isEmpty() && dq.peekFirst()<=i-k) if( !dq.isEmpty() && dq.peekFirst()<=i-k) //窗口长度过长了,删掉头
dq.removeFirst(); dq.removeFirst();
while( !dq.isEmpty() && nums[dq.peekLast()]<=nums[i]){// removeLast 不是 First。 自己写的时候这写错了,如果是First的话,有些Case也能过 while( !dq.isEmpty() && nums[dq.peekLast()]<=nums[i]){// removeLast 不是 First。 自己写的时候这写错了,如果是First的话,有些Case也能过
dq.removeLast(); dq.removeLast();
......
...@@ -5,7 +5,7 @@ package code; ...@@ -5,7 +5,7 @@ package code;
* 难度:Medium * 难度:Medium
* 分类:Binary Search, Divide and Conquer * 分类:Binary Search, Divide and Conquer
* 思路:两种方法,一种O(mlg(n)),遍历每一行,每行二分查找。另一种O(m+n),从右上角开始移动 * 思路:两种方法,一种O(mlg(n)),遍历每一行,每行二分查找。另一种O(m+n),从右上角开始移动
* Tips: * Tips:lc240, lc378
*/ */
public class lc240 { public class lc240 {
public boolean searchMatrix(int[][] matrix, int target) { public boolean searchMatrix(int[][] matrix, int target) {
......
...@@ -5,7 +5,7 @@ package code; ...@@ -5,7 +5,7 @@ package code;
* 难度:Medium * 难度:Medium
* 分类:Math, Dynamic Programming, Breadth-first Search * 分类:Math, Dynamic Programming, Breadth-first Search
* 思路:dp[i] = dp[i-j*j] +1 * 思路:dp[i] = dp[i-j*j] +1
* Tips: * Tips:lc322
*/ */
import java.util.Arrays; import java.util.Arrays;
......
...@@ -42,7 +42,7 @@ public class lc297 { ...@@ -42,7 +42,7 @@ public class lc297 {
// Decodes your encoded data to tree. // Decodes your encoded data to tree.
public TreeNode deserialize(String data) { public TreeNode deserialize(String data) {
Deque<String> nodes = new LinkedList<>(); Deque<String> nodes = new LinkedList<>();
nodes.addAll(Arrays.asList(data.split(spliter))); nodes.addAll(Arrays.asList(data.split(spliter))); //split
return buildTree(nodes); return buildTree(nodes);
} }
......
...@@ -5,6 +5,7 @@ package code; ...@@ -5,6 +5,7 @@ package code;
* 难度:Medium * 难度:Medium
* 分类:Hash Table, Two Pointers, String * 分类:Hash Table, Two Pointers, String
* 算法:两个指针,记录没有重复字母的子串的首和尾 * 算法:两个指针,记录没有重复字母的子串的首和尾
* lc76
*/ */
import java.util.HashMap; import java.util.HashMap;
......
...@@ -6,6 +6,7 @@ package code; ...@@ -6,6 +6,7 @@ package code;
* 分类:Depth-first Search, Breadth-first Search * 分类:Depth-first Search, Breadth-first Search
* 思路:先计数),如果多的话,在前边的字符里删掉一个。反转字符串,计数( * 思路:先计数),如果多的话,在前边的字符里删掉一个。反转字符串,计数(
* Tips:好难啊,里边很多细节需要注意。还有一种bfs的思路,挨个删字符,判断是否合规。 * Tips:好难啊,里边很多细节需要注意。还有一种bfs的思路,挨个删字符,判断是否合规。
* lc32, lc22, lc301
*/ */
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
......
...@@ -5,16 +5,17 @@ package code; ...@@ -5,16 +5,17 @@ package code;
* 难度:Medium * 难度:Medium
* 分类:Dynamic Programming * 分类:Dynamic Programming
* 思路:状态DP,自己不会写。要分两种状态,手中有股票时最大收益,手中没股票时最大收益(包括冷冻期)。 * 思路:状态DP,自己不会写。要分两种状态,手中有股票时最大收益,手中没股票时最大收益(包括冷冻期)。
* buy[i] means before day i what is the maxProfit for any sequence end with buy.
* buy[i] = max( buy[i-1], sell[i-2]-price[i] ) * buy[i] = max( buy[i-1], sell[i-2]-price[i] )
* sell[i] = max( sell[i-1], buy[i-1]+price[i] ) * sell[i] = max( sell[i-1], buy[i-1]+price[i] )
* 空间压缩以后时间是O(n),空间是O(1) * 压缩以后时间是O(n),空间是O(1)
* Tips:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75931/Easiest-JAVA-solution-with-explanations * Tips:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75931/Easiest-JAVA-solution-with-explanations
* lc121, lc309, lc188, lc123, lc714 * lc121, lc309, lc188, lc123, lc714
*/ */
public class lc309 { public class lc309 {
public int maxProfit(int[] prices) { public int maxProfit(int[] prices) {
if(prices.length==0) return 0; if(prices.length==0) return 0;
int b1 = -prices[0]; int b1 = -prices[0]; //注意这里的初始化
int s2=0, s1=0; int s2=0, s1=0;
int b = 0, s = 0; int b = 0, s = 0;
for (int i = 0; i < prices.length ; i++) { for (int i = 0; i < prices.length ; i++) {
......
...@@ -9,7 +9,7 @@ import java.util.List; ...@@ -9,7 +9,7 @@ import java.util.List;
* 难度:Hard * 难度:Hard
* 分类:Divide and Conquer, Binary indexed Tree, Segment Tree, Binary Search Tree * 分类:Divide and Conquer, Binary indexed Tree, Segment Tree, Binary Search Tree
* 思路:两种思路,一种用二叉搜索树这类数据结构 https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST * 思路:两种思路,一种用二叉搜索树这类数据结构 https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将ndex进行排序 * 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将index进行排序
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation * https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索 * 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code * https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code
......
...@@ -9,6 +9,7 @@ import java.util.Stack; ...@@ -9,6 +9,7 @@ import java.util.Stack;
* 分类:Dynamic Programming, String * 分类:Dynamic Programming, String
* 思路:两种常规方法,一是dp,每个位置记录以该位置结尾的最长长度。另一种是用栈,把位置索引入栈。 * 思路:两种常规方法,一是dp,每个位置记录以该位置结尾的最长长度。另一种是用栈,把位置索引入栈。
* Tips:想到了用dp,也想到了用数组记录位置结尾的解,但没有想好如何进行更新迭代计算。把位置索引入栈的方法很典型,关注一下。 * Tips:想到了用dp,也想到了用数组记录位置结尾的解,但没有想好如何进行更新迭代计算。把位置索引入栈的方法很典型,关注一下。
* lc32, lc22, lc301
*/ */
public class lc32 { public class lc32 {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -35,4 +35,19 @@ public class lc322 { ...@@ -35,4 +35,19 @@ public class lc322 {
} }
return dp[amount-1]==Integer.MAX_VALUE ? -1 : dp[amount-1]; //没解返回-1 return dp[amount-1]==Integer.MAX_VALUE ? -1 : dp[amount-1]; //没解返回-1
} }
public static int coinChange2(int[] coins, int amount) {
int max = amount + 1;
int[] dp = new int[amount + 1];
Arrays.fill(dp, max);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
} }
...@@ -4,7 +4,7 @@ package code; ...@@ -4,7 +4,7 @@ package code;
* 题意:数组中是否存在递增的3个数 * 题意:数组中是否存在递增的3个数
* 难度:Medium * 难度:Medium
* 分类:Array * 分类:Array
* 思路:思路很清奇,自己没想到,时间空间复杂度都是O(1)。其实和lc300 O(nlgn) 解法思路是一样的,只是固定了dp数组长度为两个数。 * 思路:思路很清奇,自己没想到,时间复杂度O(N),空间是O(1)。其实和lc300 O(nlgn) 解法思路是一样的,只是固定了dp数组长度为两个数。
* Tips:lc300最长递增子序列 * Tips:lc300最长递增子序列
*/ */
public class lc334 { public class lc334 {
......
...@@ -11,7 +11,7 @@ import java.util.PriorityQueue; ...@@ -11,7 +11,7 @@ import java.util.PriorityQueue;
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。 * 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
* 2是二分,二分的是val,看比这个val小的数是不是k * 2是二分,二分的是val,看比这个val小的数是不是k
* Tips:lc23方法很像 * Tips:lc23方法很像
* lc240 * lc240, lc378
*/ */
public class lc378 { public class lc378 {
class Cell{ class Cell{
......
...@@ -6,6 +6,7 @@ package code; ...@@ -6,6 +6,7 @@ package code;
* 分类:String, Dynamic Programming * 分类:String, Dynamic Programming
* Tips:从后往前遍历,保证后续dp时,子情况已计算出 * Tips:从后往前遍历,保证后续dp时,子情况已计算出
* 还有一种思路是从中间往两边扩展,中间有两种情况,一种一个字符,一种两个字符 * 还有一种思路是从中间往两边扩展,中间有两种情况,一种一个字符,一种两个字符
* lc5, lc9, lc125, lc131, lc234, lc647
*/ */
public class lc5 { public class lc5 {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -5,7 +5,7 @@ package code; ...@@ -5,7 +5,7 @@ package code;
* 难度:Medium * 难度:Medium
* 分类:String, Dynamic Programming * 分类:String, Dynamic Programming
* 思路:时间为N^2,用二维dp空间复杂度是N^2. 该题直接让判断是否回文,直接选择找中心字符,向两边拓展,空间为O(1) * 思路:时间为N^2,用二维dp空间复杂度是N^2. 该题直接让判断是否回文,直接选择找中心字符,向两边拓展,空间为O(1)
* Tips: * Tips:lc5, lc9, lc125, lc131, lc234, lc647
*/ */
public class lc647 { public class lc647 {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -5,7 +5,7 @@ package code; ...@@ -5,7 +5,7 @@ package code;
* 难度:Easy * 难度:Easy
* 分类:Math * 分类:Math
* 思路:不转换字符串的思路就是把数字反转了以后,比较是否相等 * 思路:不转换字符串的思路就是把数字反转了以后,比较是否相等
* Tips: * Tips:lc5, lc9, lc125, lc131, lc234, lc647
*/ */
public class lc9 { public class lc9 {
public boolean isPalindrome(int x) { public boolean isPalindrome(int x) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册