Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Damon-Da
leetcode
提交
87dc8f81
L
leetcode
项目概览
Damon-Da
/
leetcode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
87dc8f81
编写于
7月 01, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190701
上级
0554cec6
变更
25
隐藏空白更改
内联
并排
Showing
25 changed file
with
48 addition
and
26 deletion
+48
-26
code/lc1027.java
code/lc1027.java
+1
-1
code/lc114.java
code/lc114.java
+1
-1
code/lc125.java
code/lc125.java
+1
-1
code/lc127.java
code/lc127.java
+1
-1
code/lc131.java
code/lc131.java
+6
-1
code/lc218.java
code/lc218.java
+1
-1
code/lc22.java
code/lc22.java
+1
-0
code/lc227.java
code/lc227.java
+1
-1
code/lc234.java
code/lc234.java
+1
-0
code/lc237.java
code/lc237.java
+3
-8
code/lc239.java
code/lc239.java
+1
-1
code/lc240.java
code/lc240.java
+1
-1
code/lc279.java
code/lc279.java
+1
-1
code/lc297.java
code/lc297.java
+1
-1
code/lc3.java
code/lc3.java
+1
-0
code/lc301.java
code/lc301.java
+1
-0
code/lc309.java
code/lc309.java
+3
-2
code/lc315.java
code/lc315.java
+1
-1
code/lc32.java
code/lc32.java
+1
-0
code/lc322.java
code/lc322.java
+15
-0
code/lc334.java
code/lc334.java
+1
-1
code/lc378.java
code/lc378.java
+1
-1
code/lc5.java
code/lc5.java
+1
-0
code/lc647.java
code/lc647.java
+1
-1
code/lc9.java
code/lc9.java
+1
-1
未找到文件。
code/lc1027.java
浏览文件 @
87dc8f81
...
...
@@ -14,7 +14,7 @@ public class lc1027 {
int
[][]
dp
=
new
int
[
A
.
length
][
20000
];
for
(
int
i
=
0
;
i
<
A
.
length
;
i
++)
{
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
;
res
=
Math
.
max
(
res
,
dp
[
i
][
ind
]);
}
...
...
code/lc114.java
浏览文件 @
87dc8f81
...
...
@@ -5,7 +5,7 @@ package code;
* 难度:Medium
* 分类:Tree, Depth-first Search
* 思路:就是节点间连接换一下,理清思路就行了,类似中序遍历
* Tips:
* Tips:
lc426
*/
public
class
lc114
{
public
class
TreeNode
{
...
...
code/lc125.java
浏览文件 @
87dc8f81
...
...
@@ -6,7 +6,7 @@ package code;
* 分类:Two Pointers, String
* 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。
* Tips:记下另一种方法。第一种方法Bingo!
* lc
234, lc5
* lc
5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc125
{
public
static
void
main
(
String
[]
args
)
{
...
...
code/lc127.java
浏览文件 @
87dc8f81
...
...
@@ -29,7 +29,7 @@ public class lc127 {
curr_str
[
j
]
=
k
;
if
(
String
.
valueOf
(
curr_str
).
equals
(
endWord
))
return
level
;
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
));
}
}
...
...
code/lc131.java
浏览文件 @
87dc8f81
...
...
@@ -8,7 +8,12 @@ import java.util.List;
* 难度:Medium
* 分类:Backtracking
* 思路:典型回溯法,注意向res添加内容时要重新new一下
* Tips:lc39
* Tips: lc5, lc9, lc125, lc131, lc234, lc647
* lc39
* 判断是否为回文的方法:
* 1. 从中心往两边扩充,中心可能是一个字符,也可能是两个字符
* 2. dp,利用之前计算的结果,只判断边缘两个字符是否相等,从后往前dp
* 3. 翻转了以后,判断两个串是否相等
*/
public
class
lc131
{
public
static
void
main
(
String
[]
args
)
{
...
...
code/lc218.java
浏览文件 @
87dc8f81
...
...
@@ -39,7 +39,7 @@ public class lc218 {
int
pre
=
0
;
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
if
(
arr
[
i
][
1
]>
0
){
pr
.
add
(-
arr
[
i
][
1
]);
pr
.
add
(-
arr
[
i
][
1
]);
//默认是最小优先队列
}
else
{
pr
.
remove
(
arr
[
i
][
1
]);
}
...
...
code/lc22.java
浏览文件 @
87dc8f81
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:String, Backtracking
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
* 注意:递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
* lc32, lc22, lc301
*/
import
java.util.ArrayList
;
import
java.util.HashSet
;
...
...
code/lc227.java
浏览文件 @
87dc8f81
...
...
@@ -21,7 +21,7 @@ public class lc227 {
if
(
Character
.
isDigit
(
chs
[
i
])){
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
==
'+'
){
st
.
push
(
num
);
}
...
...
code/lc234.java
浏览文件 @
87dc8f81
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:LinkedList, Two Pointers
* 思路:反转一半就行了,避免了空间开销
* Tips:很好的题,考了 Two Pointers, 还考了链表反转
* lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc234
{
public
class
ListNode
{
...
...
code/lc237.java
浏览文件 @
87dc8f81
...
...
@@ -4,7 +4,7 @@ package code;
* 题意:删除链表中的一个节点,给的是这个节点,不知道前边的节点
* 难度:Easy
* 分类:Linked List
* 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,
倒数第二个节点置空
* 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,
删除下一个节点
* Tips:
*/
public
class
lc237
{
...
...
@@ -17,12 +17,7 @@ public class lc237 {
}
}
public
void
deleteNode
(
ListNode
node
)
{
ListNode
pre
=
new
ListNode
(-
1
);
while
(
node
.
next
!=
null
)
{
node
.
val
=
node
.
next
.
val
;
pre
=
node
;
node
=
node
.
next
;
}
pre
.
next
=
null
;
node
.
val
=
node
.
next
.
val
;
node
.
next
=
node
.
next
.
next
;
}
}
code/lc239.java
浏览文件 @
87dc8f81
...
...
@@ -27,7 +27,7 @@ public class lc239 {
int
cur
=
0
;
Deque
<
Integer
>
dq
=
new
ArrayDeque
();
//队列里是递减的
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++)
{
if
(
!
dq
.
isEmpty
()
&&
dq
.
peekFirst
()<=
i
-
k
)
if
(
!
dq
.
isEmpty
()
&&
dq
.
peekFirst
()<=
i
-
k
)
//窗口长度过长了,删掉头
dq
.
removeFirst
();
while
(
!
dq
.
isEmpty
()
&&
nums
[
dq
.
peekLast
()]<=
nums
[
i
]){
// removeLast 不是 First。 自己写的时候这写错了,如果是First的话,有些Case也能过
dq
.
removeLast
();
...
...
code/lc240.java
浏览文件 @
87dc8f81
...
...
@@ -5,7 +5,7 @@ package code;
* 难度:Medium
* 分类:Binary Search, Divide and Conquer
* 思路:两种方法,一种O(mlg(n)),遍历每一行,每行二分查找。另一种O(m+n),从右上角开始移动
* Tips:
* Tips:
lc240, lc378
*/
public
class
lc240
{
public
boolean
searchMatrix
(
int
[][]
matrix
,
int
target
)
{
...
...
code/lc279.java
浏览文件 @
87dc8f81
...
...
@@ -5,7 +5,7 @@ package code;
* 难度:Medium
* 分类:Math, Dynamic Programming, Breadth-first Search
* 思路:dp[i] = dp[i-j*j] +1
* Tips:
* Tips:
lc322
*/
import
java.util.Arrays
;
...
...
code/lc297.java
浏览文件 @
87dc8f81
...
...
@@ -42,7 +42,7 @@ public class lc297 {
// Decodes your encoded data to tree.
public
TreeNode
deserialize
(
String
data
)
{
Deque
<
String
>
nodes
=
new
LinkedList
<>();
nodes
.
addAll
(
Arrays
.
asList
(
data
.
split
(
spliter
)));
nodes
.
addAll
(
Arrays
.
asList
(
data
.
split
(
spliter
)));
//split
return
buildTree
(
nodes
);
}
...
...
code/lc3.java
浏览文件 @
87dc8f81
...
...
@@ -5,6 +5,7 @@ package code;
* 难度:Medium
* 分类:Hash Table, Two Pointers, String
* 算法:两个指针,记录没有重复字母的子串的首和尾
* lc76
*/
import
java.util.HashMap
;
...
...
code/lc301.java
浏览文件 @
87dc8f81
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:Depth-first Search, Breadth-first Search
* 思路:先计数),如果多的话,在前边的字符里删掉一个。反转字符串,计数(
* Tips:好难啊,里边很多细节需要注意。还有一种bfs的思路,挨个删字符,判断是否合规。
* lc32, lc22, lc301
*/
import
java.util.ArrayList
;
import
java.util.HashSet
;
...
...
code/lc309.java
浏览文件 @
87dc8f81
...
...
@@ -5,16 +5,17 @@ package code;
* 难度:Medium
* 分类:Dynamic Programming
* 思路:状态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] )
* 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
* lc121, lc309, lc188, lc123, lc714
*/
public
class
lc309
{
public
int
maxProfit
(
int
[]
prices
)
{
if
(
prices
.
length
==
0
)
return
0
;
int
b1
=
-
prices
[
0
];
int
b1
=
-
prices
[
0
];
//注意这里的初始化
int
s2
=
0
,
s1
=
0
;
int
b
=
0
,
s
=
0
;
for
(
int
i
=
0
;
i
<
prices
.
length
;
i
++)
{
...
...
code/lc315.java
浏览文件 @
87dc8f81
...
...
@@ -9,7 +9,7 @@ import java.util.List;
* 难度:Hard
* 分类: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
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将ndex进行排序
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将
i
ndex进行排序
* 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
...
...
code/lc32.java
浏览文件 @
87dc8f81
...
...
@@ -9,6 +9,7 @@ import java.util.Stack;
* 分类:Dynamic Programming, String
* 思路:两种常规方法,一是dp,每个位置记录以该位置结尾的最长长度。另一种是用栈,把位置索引入栈。
* Tips:想到了用dp,也想到了用数组记录位置结尾的解,但没有想好如何进行更新迭代计算。把位置索引入栈的方法很典型,关注一下。
* lc32, lc22, lc301
*/
public
class
lc32
{
public
static
void
main
(
String
[]
args
)
{
...
...
code/lc322.java
浏览文件 @
87dc8f81
...
...
@@ -35,4 +35,19 @@ public class lc322 {
}
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
];
}
}
code/lc334.java
浏览文件 @
87dc8f81
...
...
@@ -4,7 +4,7 @@ package code;
* 题意:数组中是否存在递增的3个数
* 难度:Medium
* 分类:Array
* 思路:思路很清奇,自己没想到,时间
空间复杂度都
是O(1)。其实和lc300 O(nlgn) 解法思路是一样的,只是固定了dp数组长度为两个数。
* 思路:思路很清奇,自己没想到,时间
复杂度O(N),空间
是O(1)。其实和lc300 O(nlgn) 解法思路是一样的,只是固定了dp数组长度为两个数。
* Tips:lc300最长递增子序列
*/
public
class
lc334
{
...
...
code/lc378.java
浏览文件 @
87dc8f81
...
...
@@ -11,7 +11,7 @@ import java.util.PriorityQueue;
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
* 2是二分,二分的是val,看比这个val小的数是不是k
* Tips:lc23方法很像
* lc240
* lc240
, lc378
*/
public
class
lc378
{
class
Cell
{
...
...
code/lc5.java
浏览文件 @
87dc8f81
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:String, Dynamic Programming
* Tips:从后往前遍历,保证后续dp时,子情况已计算出
* 还有一种思路是从中间往两边扩展,中间有两种情况,一种一个字符,一种两个字符
* lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc5
{
public
static
void
main
(
String
[]
args
)
{
...
...
code/lc647.java
浏览文件 @
87dc8f81
...
...
@@ -5,7 +5,7 @@ package code;
* 难度:Medium
* 分类:String, Dynamic Programming
* 思路:时间为N^2,用二维dp空间复杂度是N^2. 该题直接让判断是否回文,直接选择找中心字符,向两边拓展,空间为O(1)
* Tips:
* Tips:
lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc647
{
public
static
void
main
(
String
[]
args
)
{
...
...
code/lc9.java
浏览文件 @
87dc8f81
...
...
@@ -5,7 +5,7 @@ package code;
* 难度:Easy
* 分类:Math
* 思路:不转换字符串的思路就是把数字反转了以后,比较是否相等
* Tips:
* Tips:
lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc9
{
public
boolean
isPalindrome
(
int
x
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录