Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
NoComments
leetcode
提交
34e359cf
L
leetcode
项目概览
NoComments
/
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,体验更适合开发者的 AI 搜索 >>
提交
34e359cf
编写于
7月 03, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190703
上级
87dc8f81
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
54 addition
and
13 deletion
+54
-13
code/lc1026.java
code/lc1026.java
+13
-2
code/lc1094.java
code/lc1094.java
+1
-1
code/lc124.java
code/lc124.java
+1
-1
code/lc146.java
code/lc146.java
+1
-0
code/lc297.java
code/lc297.java
+1
-1
code/lc378.java
code/lc378.java
+1
-1
code/lc380.java
code/lc380.java
+1
-1
code/lc395.java
code/lc395.java
+4
-4
code/lc416.java
code/lc416.java
+3
-0
code/lc437.java
code/lc437.java
+1
-0
code/lc572.java
code/lc572.java
+2
-0
code/lc921.java
code/lc921.java
+1
-1
code/lc978.java
code/lc978.java
+24
-1
未找到文件。
code/lc1026.java
浏览文件 @
34e359cf
package
code
;
/*
* 1025.
Divisor Game
* 1025.
Maximum Difference Between Node and Ancestor
* 题意:父节点减子节点的绝对值最大
* 难度:
* 分类:
* 思路:自己写的自下向上,返回的时候再计算。
* 可以自顶向下的,
到叶子节点计算就可以
* 可以自顶向下的,
把路径上的最大值和最小值传到叶子节点,到叶子节点再计算
* Tips:
*/
public
class
lc1026
{
...
...
@@ -44,4 +44,15 @@ public class lc1026 {
res
[
1
]
=
Math
.
min
(
Math
.
min
(
left
[
1
],
right
[
1
]),
root
.
val
);
return
res
;
}
public
int
maxAncestorDiff2
(
TreeNode
root
)
{
return
dfs
(
root
,
root
.
val
,
root
.
val
);
}
public
int
dfs
(
TreeNode
root
,
int
mn
,
int
mx
)
{
if
(
root
==
null
)
return
mx
-
mn
;
mx
=
Math
.
max
(
mx
,
root
.
val
);
mn
=
Math
.
min
(
mn
,
root
.
val
);
return
Math
.
max
(
dfs
(
root
.
left
,
mn
,
mx
),
dfs
(
root
.
right
,
mn
,
mx
));
}
}
code/lc1094.java
浏览文件 @
34e359cf
...
...
@@ -4,7 +4,7 @@ package code;
* 题意:
* 难度:Medium
* 分类:
* 思路:lc56 类似的题,但该题的集合没有传递覆盖的特性,所以遍历的时候又加了个循环,N^2
* 思路:
lc56 类似的题,但该题的集合没有传递覆盖的特性,所以遍历的时候又加了个循环,N^2
* 非常巧妙的方法,每次记录上车,下车人数,然后遍历一遍进行模拟上下车
* Tips:lc253, lc56
*/
...
...
code/lc124.java
浏览文件 @
34e359cf
...
...
@@ -6,7 +6,7 @@ package code;
* 分类:Tree, Depth-first Search
* 思路:因为二叉树只有两个节点,一条路径可以想象成倒V字,从低层的某个节点一路向上,到达一个顶点,再一路向下,理解了这一点,整道题就好解了。
* Tips:用了一个全局变量存储最后结果,因为函数返回的是直线路径上的最优解,而不是V字路径最优解
* lc112, lc113, lc437, lc129, lc124, lc337, lc543
* lc112, lc113, lc437, lc129, lc124, lc337, lc543
, lc1026
*/
public
class
lc124
{
public
class
TreeNode
{
...
...
code/lc146.java
浏览文件 @
34e359cf
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:Design
* 思路:hashmap + 双向链表。hashmap实现了O(1)的get,双向链表实现O(1)的put
* Tips:能想到双向链表,就不难了
* lc380
*/
import
java.util.HashMap
;
...
...
code/lc297.java
浏览文件 @
34e359cf
...
...
@@ -10,7 +10,7 @@ import java.util.LinkedList;
* 难度:Hard
* 分类:Tree, Design
* 思路:
* Tips:
* Tips:
lc572 序列化的应用
*/
public
class
lc297
{
public
class
TreeNode
{
...
...
code/lc378.java
浏览文件 @
34e359cf
...
...
@@ -5,7 +5,7 @@ import java.util.PriorityQueue;
/*
* 378. Kth Smallest Element in a Sorted Matrix
* 题意:在矩阵中搜索第k
大
的数,横轴和纵轴都是有序的
* 题意:在矩阵中搜索第k
小
的数,横轴和纵轴都是有序的
* 难度:Medium
* 分类:Binary Search, Heap
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
...
...
code/lc380.java
浏览文件 @
34e359cf
...
...
@@ -10,7 +10,7 @@ import java.util.List;
* 难度:Medium
* 分类:Array, Hash Table, Design
* 思路:List 的插入和删除都是O(1), 通过hashmap绑定来使得Get也为O(1)
* Tips:
* Tips:
和LRU哪个题类似 lc146
*/
public
class
lc380
{
public
class
RandomizedSet
{
...
...
code/lc395.java
浏览文件 @
34e359cf
...
...
@@ -13,21 +13,21 @@ public class lc395 {
public
int
longestSubstring
(
String
s
,
int
k
)
{
int
res
=
0
;
for
(
int
i
=
1
;
i
<=
26
;
i
++)
{
int
left
=
0
,
right
=
0
,
cur_uni_char
=
0
,
less
_than_k_char
=
0
;
int
left
=
0
,
right
=
0
,
cur_uni_char
=
0
,
more
_than_k_char
=
0
;
int
[]
map
=
new
int
[
26
];
while
(
right
<
s
.
length
()){
//右边推进
map
[
s
.
charAt
(
right
)-
'a'
]++;
if
(
map
[
s
.
charAt
(
right
)-
'a'
]==
k
)
less
_than_k_char
++;
if
(
map
[
s
.
charAt
(
right
)-
'a'
]==
k
)
more
_than_k_char
++;
if
(
map
[
s
.
charAt
(
right
)-
'a'
]==
1
)
cur_uni_char
++;
right
++;
if
(
cur_uni_char
==
i
&&
less
_than_k_char
==
i
)
res
=
Math
.
max
(
res
,
right
-
left
);
if
(
cur_uni_char
==
i
&&
more
_than_k_char
==
i
)
res
=
Math
.
max
(
res
,
right
-
left
);
else
if
(
cur_uni_char
>
i
){
//左边推进。不在外边加上一个循环的话,就不知道怎么推荐左指针了。
while
(
cur_uni_char
!=
i
){
map
[
s
.
charAt
(
left
)-
'a'
]--;
if
(
map
[
s
.
charAt
(
left
)-
'a'
]==
0
)
cur_uni_char
--;
if
((
map
[
s
.
charAt
(
left
)-
'a'
]==
k
-
1
))
less
_than_k_char
--;
if
((
map
[
s
.
charAt
(
left
)-
'a'
]==
k
-
1
))
more
_than_k_char
--;
left
++;
}
}
...
...
code/lc416.java
浏览文件 @
34e359cf
...
...
@@ -7,6 +7,7 @@ import java.util.HashSet;
* 难度:Medium
* 分类:Dynamic Programming
* 思路:题意可以转换为用任意个元素组成的和等于数组和/2。可以和 lc1, lc15 3-Sum 对比。
* dfs过不了,2^n
* 0,1背包问题,递推比较简单,所以空间可以压缩成一维
* 自己想的思路其实和压缩后的0,1背包类似,但没想到该问题可以抽象为0,1背包
* dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
...
...
@@ -67,4 +68,6 @@ public class lc416 {
}
return
dp
[
volumn
];
}
}
code/lc437.java
浏览文件 @
34e359cf
...
...
@@ -13,6 +13,7 @@ import java.util.HashMap;
* 和lc560有共同的思想,每个节点只需遍历一遍就可以了
* 虽然是Easy题,做好也不简单
* lc112, lc113, lc437, lc129, lc124, lc337
* lc437 lc572 一样的递归思路
* lc303, lc437, lc560
*/
public
class
lc437
{
...
...
code/lc572.java
浏览文件 @
34e359cf
...
...
@@ -6,6 +6,8 @@ package code;
* 分类:Tree
* 思路:两种方法,一种是先序遍历,然后比较字符串即可,注意每个节点开始前加个字符,null也要加进去。
* 另一种递归的方法。
* lc437 lc572 一样的递归思路
* lc297 序列化
* Tips:
*/
public
class
lc572
{
...
...
code/lc921.java
浏览文件 @
34e359cf
...
...
@@ -7,7 +7,7 @@ import java.util.Stack;
* 难度:Medium
* 分类:Stack, Greedy
* 思路:匹配的都出栈,最后剩下的栈中没匹配的个数,就是需要添加的个数
* Tips:
* Tips:
lc301
*/
public
class
lc921
{
public
int
minAddToMakeValid
(
String
S
)
{
...
...
code/lc978.java
浏览文件 @
34e359cf
...
...
@@ -8,7 +8,7 @@ package code;
* Tips:lc53
*/
public
class
lc978
{
public
int
maxTurbulenceSize
(
int
[]
A
)
{
public
int
maxTurbulenceSize
(
int
[]
A
)
{
//太难想了
int
inc
=
1
,
dec
=
1
,
result
=
1
;
for
(
int
i
=
1
;
i
<
A
.
length
;
i
++)
{
if
(
A
[
i
]
<
A
[
i
-
1
])
{
// +1 并且重置另一个统计量
...
...
@@ -25,4 +25,27 @@ public class lc978 {
}
return
result
;
}
public
int
maxTurbulenceSize2
(
int
[]
A
)
{
int
[]
arr
=
new
int
[
A
.
length
-
1
];
for
(
int
i
=
1
;
i
<
A
.
length
;
i
++){
if
(
A
[
i
]==
A
[
i
-
1
])
arr
[
i
-
1
]
=
0
;
else
if
(
A
[
i
]>
A
[
i
-
1
])
arr
[
i
-
1
]
=
1
;
else
arr
[
i
-
1
]
=
-
1
;
}
int
res
=
1
;
boolean
flag
=
false
;
//判断下是否全是0, 返回的时候就不+1
int
count
=
1
;
for
(
int
i
=
1
;
i
<
arr
.
length
;
i
++){
if
(
arr
[
i
]!=
0
)
flag
=
true
;
if
(
arr
[
i
]==-
arr
[
i
-
1
]
&&
arr
[
i
]!=
0
)
{
count
++;
}
else
{
res
=
Math
.
max
(
res
,
count
);
count
=
1
;
}
}
res
=
Math
.
max
(
res
,
count
);
return
flag
?
res
+
1
:
res
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录