Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Damon-Da
leetcode
提交
ea4ea28e
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,发现更多精彩内容 >>
提交
ea4ea28e
编写于
8月 18, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190818
上级
4ca0257b
变更
23
隐藏空白更改
内联
并排
Showing
23 changed file
with
175 addition
and
52 deletion
+175
-52
.gitignore
.gitignore
+2
-1
code/lc1026.java
code/lc1026.java
+1
-1
code/lc105.java
code/lc105.java
+13
-0
code/lc113.java
code/lc113.java
+1
-1
code/lc148.java
code/lc148.java
+2
-2
code/lc149.java
code/lc149.java
+1
-1
code/lc153.java
code/lc153.java
+1
-1
code/lc22.java
code/lc22.java
+1
-1
code/lc236.java
code/lc236.java
+2
-4
code/lc239.java
code/lc239.java
+1
-1
code/lc300.java
code/lc300.java
+2
-4
code/lc315.java
code/lc315.java
+1
-0
code/lc324.java
code/lc324.java
+2
-20
code/lc384.java
code/lc384.java
+1
-1
code/lc395.java
code/lc395.java
+1
-1
code/lc416.java
code/lc416.java
+1
-0
code/lc42.java
code/lc42.java
+1
-1
code/lc493.java
code/lc493.java
+2
-2
code/lc572.java
code/lc572.java
+4
-5
code/lc617.java
code/lc617.java
+2
-2
code/lc84.java
code/lc84.java
+3
-3
interview/bk2.java
interview/bk2.java
+28
-0
interview/bk4.java
interview/bk4.java
+102
-0
未找到文件。
.gitignore
浏览文件 @
ea4ea28e
.DS_Store
*/.DS_Store
/
kickstart/*
kickstart/*
test.java
interview/*
code/lc1026.java
浏览文件 @
ea4ea28e
package
code
;
/*
* 102
5
. Maximum Difference Between Node and Ancestor
* 102
6
. Maximum Difference Between Node and Ancestor
* 题意:父节点减子节点的绝对值最大
* 难度:
* 分类:
...
...
code/lc105.java
浏览文件 @
ea4ea28e
...
...
@@ -37,4 +37,17 @@ public class lc105 {
return
tn
;
//记住函数的返回值的设置,返回Node,递归的构造子树
}
public
TreeNode
buildTree2
(
int
[]
preorder
,
int
[]
inorder
)
{
return
helper
(
preorder
,
inorder
,
0
,
0
,
preorder
.
length
-
1
);
}
public
TreeNode
helper
(
int
[]
preorder
,
int
[]
inorder
,
int
cur
,
int
left
,
int
right
){
if
(
left
>
right
)
return
null
;
TreeNode
tn
=
new
TreeNode
(
preorder
[
cur
]);
int
i
=
left
;
for
(;
i
<=
right
;
i
++)
if
(
inorder
[
i
]==
preorder
[
cur
])
break
;
tn
.
left
=
helper
(
preorder
,
inorder
,
cur
+
1
,
left
,
i
-
1
);
tn
.
right
=
helper
(
preorder
,
inorder
,
cur
+
i
-
left
+
1
,
i
+
1
,
right
);
return
tn
;
}
}
code/lc113.java
浏览文件 @
ea4ea28e
...
...
@@ -33,7 +33,7 @@ public class lc113 {
helper
(
res
,
cur
,
root
.
left
,
sum
-
root
.
val
);
helper
(
res
,
cur
,
root
.
right
,
sum
-
root
.
val
);
}
cur
.
remove
(
cur
.
size
()-
1
);
cur
.
remove
(
cur
.
size
()-
1
);
//注意是去掉最后一个,传的是索引。传递对象的话,序列可能会变。
return
;
}
}
code/lc148.java
浏览文件 @
ea4ea28e
...
...
@@ -75,9 +75,9 @@ public class lc148 {
public
ListNode
partion
(
ListNode
head
,
ListNode
end
)
{
ListNode
p1
=
head
,
p2
=
head
.
next
;
//p1指的是小于pivot的索引
//走到末尾才停
while
(
p2
!=
end
)
{
//p1与p2间都是大于pivot的数
while
(
p2
!=
end
)
{
//
head到p1间是小于pivot的数,
p1与p2间都是大于pivot的数
if
(
p2
.
val
<
head
.
val
)
{
//lc922 类似的思想, 把小于的值放到该放的位置上
p1
=
p1
.
next
;
...
...
code/lc149.java
浏览文件 @
ea4ea28e
...
...
@@ -20,7 +20,7 @@ public class lc149 {
public
int
maxPoints
(
Point
[]
points
)
{
if
(
points
.
length
<=
2
)
return
points
.
length
;
int
res
=
0
;
for
(
int
i
=
0
;
i
<
points
.
length
-
1
;
i
++)
{
for
(
int
i
=
0
;
i
<
points
.
length
-
1
;
i
++)
{
//先固定一个点,再根据斜率进行记录就行了
HashMap
<
Integer
,
HashMap
<
Integer
,
Integer
>>
hm
=
new
HashMap
<>();
int
overlap
=
1
;
int
max
=
0
;
...
...
code/lc153.java
浏览文件 @
ea4ea28e
...
...
@@ -16,7 +16,7 @@ public class lc153 {
int
mid
=
(
left
+
right
)/
2
;
if
(
nums
[
mid
]<
nums
[
right
]){
right
=
mid
;
//这不加1
}
else
if
(
nums
[
mid
]>
nums
[
right
])
{
}
else
{
left
=
mid
+
1
;
}
}
...
...
code/lc22.java
浏览文件 @
ea4ea28e
package
code
;
/*
* 22. Generate Parentheses
* 题意:正确括号组合
的
* 题意:正确括号组合
* 难度:Medium
* 分类:String, Backtracking
* 思路:回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
...
...
code/lc236.java
浏览文件 @
ea4ea28e
...
...
@@ -23,10 +23,8 @@ public class lc236 {
return
root
;
TreeNode
left
=
lowestCommonAncestor
(
root
.
left
,
p
,
q
);
TreeNode
right
=
lowestCommonAncestor
(
root
.
right
,
p
,
q
);
if
(
left
!=
null
&&
right
!=
null
)
//回溯返回。哪边不为空,返回哪边,否则返回自己。
return
root
;
else
if
(
left
!=
null
)
return
left
;
if
(
left
!=
null
&&
right
!=
null
)
return
root
;
//回溯返回。哪边不为空,返回哪边,否则返回自己。
else
if
(
left
!=
null
)
return
left
;
else
return
right
;
}
...
...
code/lc239.java
浏览文件 @
ea4ea28e
...
...
@@ -25,7 +25,7 @@ public class lc239 {
return
new
int
[]{};
int
[]
res
=
new
int
[
nums
.
length
-
k
+
1
];
int
cur
=
0
;
Deque
<
Integer
>
dq
=
new
ArrayDeque
();
//队列里是递减的
Deque
<
Integer
>
dq
=
new
ArrayDeque
();
//队列里是递减的
,存的仍然是下标
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++)
{
if
(
!
dq
.
isEmpty
()
&&
dq
.
peekFirst
()<=
i
-
k
)
//窗口长度过长了,删掉头
dq
.
removeFirst
();
...
...
code/lc300.java
浏览文件 @
ea4ea28e
...
...
@@ -38,10 +38,8 @@ public class lc300 {
int
right
=
size
;
while
(
left
!=
right
){
//得到要插入的位置
int
mid
=
(
left
+
right
)/
2
;
if
(
dp
[
mid
]<
nums
[
i
])
left
=
mid
+
1
;
else
right
=
mid
;
if
(
dp
[
mid
]<
nums
[
i
])
left
=
mid
+
1
;
//这是+1记住,不能到else去-1, 会死循环。+1就超出边界,后续用left赋值
else
right
=
mid
;
}
dp
[
left
]
=
nums
[
i
];
if
(
left
==
size
)
size
++;
...
...
code/lc315.java
浏览文件 @
ea4ea28e
...
...
@@ -14,6 +14,7 @@ import java.util.List;
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索,插入其实是不行的,因为插入操作以后还要移动value位置,又是一个O(N)的操作
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code
* Tips:好难呀,我日!
* lc493
*/
public
class
lc315
{
class
TreeNode
{
...
...
code/lc324.java
浏览文件 @
ea4ea28e
...
...
@@ -20,7 +20,7 @@ public class lc324 {
for
(
int
i
=
0
,
j
=
0
,
k
=
n
-
1
;
j
<=
k
;)
{
// <medium的放左边, >在右边
if
(
copy
[
j
]
<
median
)
{
swap
(
copy
,
i
++,
j
++);
swap
(
copy
,
j
++,
i
++);
}
else
if
(
copy
[
j
]
>
median
)
{
swap
(
copy
,
j
,
k
--);
}
else
{
...
...
@@ -32,27 +32,13 @@ public class lc324 {
for
(
int
i
=
n
-
1
,
j
=
1
;
i
>=
m
;
i
--,
j
+=
2
)
nums
[
j
]
=
copy
[
i
];
}
private
int
getMiddle
(
int
[]
nums
,
int
l
,
int
r
)
{
int
i
=
l
;
for
(
int
j
=
l
+
1
;
j
<=
r
;
j
++)
{
if
(
nums
[
j
]
<
nums
[
l
])
swap
(
nums
,
++
i
,
j
);
}
swap
(
nums
,
l
,
i
);
return
i
;
}
private
void
swap
(
int
[]
nums
,
int
i
,
int
j
)
{
int
t
=
nums
[
i
];
nums
[
i
]
=
nums
[
j
];
nums
[
j
]
=
t
;
}
public
static
int
findMedium
(
int
[]
nums
,
int
left
,
int
right
,
int
k
){
public
int
findMedium
(
int
[]
nums
,
int
left
,
int
right
,
int
k
){
int
cur
=
nums
[
left
];
int
l
=
left
;
int
r
=
right
;
...
...
@@ -70,8 +56,4 @@ public class lc324 {
else
if
(
left
>=
k
)
return
findMedium
(
nums
,
l
,
right
-
1
,
k
);
else
return
findMedium
(
nums
,
right
+
1
,
r
,
k
);
}
public
static
int
newIndex
(
int
index
,
int
n
)
{
return
(
1
+
2
*
index
)
%
(
n
|
1
);
}
}
code/lc384.java
浏览文件 @
ea4ea28e
...
...
@@ -25,7 +25,7 @@ public class lc384 {
public
int
[]
shuffle
()
{
int
[]
rand
=
new
int
[
nums
.
length
];
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
int
r
=
(
int
)
(
Math
.
random
()
*
(
i
+
1
));
int
r
=
(
int
)
(
Math
.
random
()
*
(
i
+
1
));
// +1是因为下标从0开始
rand
[
i
]
=
rand
[
r
];
rand
[
r
]
=
nums
[
i
];
}
...
...
code/lc395.java
浏览文件 @
ea4ea28e
...
...
@@ -23,7 +23,7 @@ public class lc395 {
if
(
cur_uni_char
==
i
&&
more_than_k_char
==
i
)
res
=
Math
.
max
(
res
,
right
-
left
);
else
if
(
cur_uni_char
>
i
){
//左边推进。不在外边加上一个循环的话,就不知道怎么推
荐
左指针了。
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
--;
...
...
code/lc416.java
浏览文件 @
ea4ea28e
...
...
@@ -11,6 +11,7 @@ import java.util.HashSet;
* 0,1背包问题,递推比较简单,所以空间可以压缩成一维
* 自己想的思路其实和压缩后的0,1背包类似,但没想到该问题可以抽象为0,1背包
* dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
* i表示数组长度,j表示求和为j
* Tips:lc416, lc494
*/
public
class
lc416
{
...
...
code/lc42.java
浏览文件 @
ea4ea28e
...
...
@@ -52,7 +52,7 @@ public class lc42 {
int
i
=
0
,
maxWater
=
0
,
maxBotWater
=
0
;
while
(
i
<
A
.
length
){
if
(
s
.
isEmpty
()
||
A
[
i
]<=
A
[
s
.
peek
()]){
s
.
push
(
i
++);
s
.
push
(
i
++);
//递减栈
}
else
{
int
bot
=
s
.
pop
();
...
...
code/lc493.java
浏览文件 @
ea4ea28e
...
...
@@ -6,11 +6,11 @@ import java.util.Arrays;
* 493. Reverse Pairs
* 题意:逆序对
* 难度:Hard
* 分类:
* 分类:
Binary Search, Divide and Conquer, Sort, Binary Indexed Tree, Segment Tree
* 思路:归并排序的思路 或者 树相关的数据结构
* 排序前先count
* 负数怎么解决? 不用考虑,因为排序前先count
* Tips:
* Tips:
lc315
*/
public
class
lc493
{
public
static
void
main
(
String
[]
args
)
{
...
...
code/lc572.java
浏览文件 @
ea4ea28e
...
...
@@ -19,13 +19,12 @@ public class lc572 {
}
public
boolean
isSubtree
(
TreeNode
s
,
TreeNode
t
)
{
if
(
s
==
null
)
return
false
;
return
helper
(
s
,
t
)
||
isSubtree
(
s
.
left
,
t
)
||
isSubtree
(
s
.
right
,
t
);
// 注意递归方法的不同,是调用哪个函数
if
(
s
==
null
||
t
==
null
)
return
s
==
t
;
return
helper
(
s
,
t
)
||
isSubtree
(
s
.
left
,
t
)
||
isSubtree
(
s
.
right
,
t
);
// 注意递归方法的不同,是调用哪个函数
}
public
boolean
helper
(
TreeNode
s
,
TreeNode
t
){
if
(
s
==
null
&&
t
==
null
)
return
true
;
if
(
s
==
null
||
t
==
null
||
s
.
val
!=
t
.
val
)
return
false
;
return
helper
(
s
.
left
,
t
.
left
)
&&
helper
(
s
.
right
,
t
.
right
);
if
(
s
==
null
||
t
==
null
)
return
s
==
t
;
return
s
.
val
==
t
.
val
&&
helper
(
s
.
left
,
t
.
left
)
&&
helper
(
s
.
right
,
t
.
right
);
}
public
boolean
isSubtree2
(
TreeNode
s
,
TreeNode
t
)
{
...
...
code/lc617.java
浏览文件 @
ea4ea28e
...
...
@@ -44,8 +44,8 @@ public class lc617 {
public
TreeNode
mergeTrees2
(
TreeNode
t1
,
TreeNode
t2
)
{
if
(
t1
==
null
)
return
t2
;
//这里注意一下,比较难想明白,可以记一下
if
(
t2
==
null
)
return
t1
;
t1
.
left
=
mergeTrees
(
t1
.
left
,
t2
.
left
);
t1
.
right
=
mergeTrees
(
t1
.
right
,
t2
.
right
);
t1
.
left
=
mergeTrees
(
t1
.
left
,
t2
.
left
);
//搞定下层指针
t1
.
right
=
mergeTrees
(
t1
.
right
,
t2
.
right
);
//前边判断过了,两个都不为null
t1
.
val
+=
t2
.
val
;
return
t1
;
}
...
...
code/lc84.java
浏览文件 @
ea4ea28e
...
...
@@ -27,14 +27,14 @@ public class lc84 {
if
(
st
.
size
()==
0
||
h
>
heights
[
st
.
peek
()]
){
//递增入栈,保证栈内索引对应的Height递增
st
.
push
(
i
);
}
else
{
int
n
=
st
.
pop
();
int
n
=
st
.
pop
();
//计算该位置height高度的矩形
int
left
;
if
(
st
.
isEmpty
())
left
=
-
1
;
//若为空,则到最左边
else
left
=
st
.
peek
();
res
=
Math
.
max
(
res
,
heights
[
n
]
*
(
i
-
left
-
1
));
//i之前的,要-1
i
--;
res
=
Math
.
max
(
res
,
heights
[
n
]
*
(
i
-
left
-
1
));
//i之前的,要-1
; 注意是height[n], 不是height[i]
i
--;
//注意i--,相当于循环出栈,总体复杂度还是O(n),因为栈最大是heights.len
}
}
return
res
;
...
...
interview/bk2.java
0 → 100644
浏览文件 @
ea4ea28e
package
interview
;
public
class
bk2
{
public
static
void
main
(
String
[]
args
)
{
}
public
int
lengthOfLIS2
(
int
[]
nums
)
{
if
(
nums
.
length
<
2
)
return
nums
.
length
;
int
size
=
0
;
//size指dp中递增的长度。 dp[0~i] 表示了长度为 i+1 的递增子数组,且最后一个值是最小值
int
[]
dp
=
new
int
[
nums
.
length
];
//dp存储递增的数组,之后更新这个数组。如果x>最后一个值,则插入到末尾,否则更新对应位置上的值为该值。
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++)
{
int
left
=
0
;
int
right
=
size
;
while
(
left
!=
right
){
//得到要插入的位置
int
mid
=
(
left
+
right
)/
2
;
if
(
dp
[
mid
]<
nums
[
i
])
left
=
mid
+
1
;
else
right
=
mid
;
}
dp
[
left
]
=
nums
[
i
];
if
(
left
==
size
)
size
++;
}
return
size
;
}
}
interview/bk4.java
0 → 100644
浏览文件 @
ea4ea28e
package
interview
;
import
java.util.Scanner
;
public
class
bk4
{
public
static
void
main
(
String
[]
args
)
{
//int[] arr = new int[]{1,4,3,2,5};
int
[]
arr
=
new
int
[]{
1
,
2
,
3
,
4
,
5
,
8
};
//int[] arr = new int[]{8,7,6,5,4,3};
System
.
out
.
println
(
func
(
arr
));
}
public
static
int
func
(
int
[]
arr
){
int
res
=
Integer
.
MAX_VALUE
;
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
int
cur
=
0
;
int
max1
=
-
1
;
int
j
=
0
;
for
(;
j
<
i
;
j
++)
{
max1
=
Math
.
max
(
max1
+
1
,
j
>
0
?
arr
[
j
-
1
]:
0
);
cur
+=
Math
.
max
(
max1
-
arr
[
j
]+
1
,
0
);
}
max1
=
Math
.
max
(
max1
,
j
>
0
?
arr
[
j
-
1
]:
0
);
int
max2
=
-
1
;
j
=
arr
.
length
-
1
;
for
(;
j
>
i
;
j
--)
{
max2
=
Math
.
max
(
max2
+
1
,
j
<
arr
.
length
-
1
?
arr
[
j
+
1
]:
0
);
cur
+=
Math
.
max
(
max2
-
arr
[
j
]
+
1
,
0
);
}
max2
=
Math
.
max
(
max2
,
j
<
arr
.
length
-
1
?
arr
[
j
+
1
]:
0
);
cur
+=
Math
.
max
(
Math
.
max
(
max1
,
max2
)
+
1
-
arr
[
i
],
0
);
res
=
Math
.
min
(
res
,
cur
);
}
return
res
;
}
public
static
void
main2
(
String
[]
args
)
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
int
n
=
scanner
.
nextInt
();
int
[]
nums
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
nums
[
i
]
=
scanner
.
nextInt
();
}
int
[]
temp
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
temp
[
i
]
=
nums
[
i
];
}
int
count
=
Integer
.
MAX_VALUE
;
int
sum
=
0
;
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
if
(
nums
[
i
]
<=
nums
[
i
-
1
])
{
sum
+=
nums
[
i
-
1
]
+
1
-
nums
[
i
];
nums
[
i
]
=
nums
[
i
-
1
]
+
1
;
}
}
if
(
sum
<
count
)
count
=
sum
;
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
nums
[
i
]
=
temp
[
i
];
}
sum
=
0
;
for
(
int
j
=
n
-
2
;
j
>=
0
;
j
--)
{
if
(
nums
[
j
]
<=
nums
[
j
+
1
])
{
sum
+=
nums
[
j
+
1
]
+
1
-
nums
[
j
];
nums
[
j
]
=
nums
[
j
+
1
]
+
1
;
}
}
if
(
sum
<
count
)
count
=
sum
;
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
nums
[
i
]
=
temp
[
i
];
}
for
(
int
i
=
1
;
i
<
n
-
1
;
i
++)
{
sum
=
0
;
int
j
=
1
;
for
(;
j
<
i
;
j
++)
{
if
(
nums
[
j
]
<=
nums
[
j
-
1
])
{
sum
+=
nums
[
j
-
1
]
+
1
-
nums
[
j
];
nums
[
j
]
=
nums
[
j
-
1
]
+
1
;
}
}
j
--;
int
k
=
n
-
2
;
for
(;
k
>
i
;
k
--)
{
if
(
nums
[
k
]
<=
nums
[
k
+
1
])
{
sum
+=
nums
[
k
+
1
]
+
1
-
nums
[
k
];
nums
[
k
]
=
nums
[
k
+
1
]
+
1
;
}
}
k
++;
int
res
=
Math
.
max
(
nums
[
j
],
nums
[
k
]);
if
(
nums
[
i
]
<=
res
)
{
sum
+=
res
+
1
-
nums
[
i
];
}
if
(
sum
<
count
)
count
=
sum
;
for
(
int
l
=
0
;
l
<
n
;
l
++)
{
nums
[
l
]
=
temp
[
l
];
}
}
System
.
out
.
println
(
count
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录