Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
NoComments
leetcode
提交
abfdac66
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,发现更多精彩内容 >>
提交
abfdac66
编写于
7月 11, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190711
上级
372335e7
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
93 addition
and
20 deletion
+93
-20
code/lc148.java
code/lc148.java
+2
-2
code/lc153.java
code/lc153.java
+6
-13
code/lc206.java
code/lc206.java
+1
-0
code/lc236.java
code/lc236.java
+2
-1
code/lc25.java
code/lc25.java
+39
-0
code/lc300.java
code/lc300.java
+1
-1
code/lc315.java
code/lc315.java
+1
-1
code/lc72.java
code/lc72.java
+1
-1
code/lc75.java
code/lc75.java
+1
-1
code/lc912.java
code/lc912.java
+39
-0
未找到文件。
code/lc148.java
浏览文件 @
abfdac66
...
...
@@ -27,12 +27,12 @@ public class lc148 {
}
ListNode
slow
=
head
;
ListNode
fast
=
head
.
next
;
while
(
fast
.
next
!=
null
&&
fast
.
nex
t
.
next
!=
null
){
//把链表分成两半
while
(
fast
!=
null
&&
fas
t
.
next
!=
null
){
//把链表分成两半
slow
=
slow
.
next
;
fast
=
fast
.
next
.
next
;
}
ListNode
l2
=
sortList
(
slow
.
next
);
slow
.
next
=
null
;
slow
.
next
=
null
;
//别忘了这要断开
ListNode
l1
=
sortList
(
head
);
return
mergeList
(
l1
,
l2
);
}
...
...
code/lc153.java
浏览文件 @
abfdac66
...
...
@@ -4,8 +4,9 @@ package code;
* 题意:反转数组找最小值
* 难度:Medium
* 分类:Array, Binary Search
* 思路:
* 思路:
想清楚,不用那么多判断
* Tips:边界条件想清楚
* nums[mid]和nums[right]比就行了
*/
public
class
lc153
{
public
int
findMin
(
int
[]
nums
)
{
...
...
@@ -13,18 +14,10 @@ public class lc153 {
int
right
=
nums
.
length
-
1
;
while
(
left
<
right
){
int
mid
=
(
left
+
right
)/
2
;
if
(
nums
[
left
]<
nums
[
mid
]){
//左边有序
if
(
nums
[
left
]<
nums
[
right
]){
//在左边找
right
=
mid
-
1
;
}
else
{
left
=
mid
;
}
}
else
{
//右边有序
if
(
nums
[
right
]<
nums
[
mid
]){
//边界条件想清楚
left
=
mid
+
1
;
}
else
{
right
=
mid
;
}
if
(
nums
[
mid
]<
nums
[
right
]){
right
=
mid
;
//这不加1
}
else
if
(
nums
[
mid
]>
nums
[
right
]){
left
=
mid
+
1
;
}
}
return
nums
[
left
];
...
...
code/lc206.java
浏览文件 @
abfdac66
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:Linked List
* 思路:2中方法:设置一个快走一步的快指针,注意赋值操作顺序。还有一种递归的方法。
* Tips:递归的方法有点绕,多看下
* lc25, lc206
*/
public
class
lc206
{
public
class
ListNode
{
...
...
code/lc236.java
浏览文件 @
abfdac66
...
...
@@ -9,6 +9,7 @@ import java.util.*;
* 分类:Tree
* 思路:递归,迭代两种方法
* Tips:注意递归时怎么返回。很经典的题目。
*
*/
public
class
lc236
{
public
class
TreeNode
{
...
...
@@ -18,7 +19,7 @@ public class lc236 {
TreeNode
(
int
x
)
{
val
=
x
;
}
}
public
TreeNode
lowestCommonAncestor
(
TreeNode
root
,
TreeNode
p
,
TreeNode
q
)
{
//递归
if
(
root
==
null
||
root
==
p
||
root
==
q
)
if
(
root
==
null
||
root
==
p
||
root
==
q
)
//注意这个条件
return
root
;
TreeNode
left
=
lowestCommonAncestor
(
root
.
left
,
p
,
q
);
TreeNode
right
=
lowestCommonAncestor
(
root
.
right
,
p
,
q
);
...
...
code/lc25.java
0 → 100644
浏览文件 @
abfdac66
package
code
;
/*
* 25. Reverse Nodes in k-Group
* 题意:每k个反转一下,不足k的不反转,直接接上
* 难度:Hard
* 分类:Linked List
* 思路:递归调用反转,反转完下一段的返回节点,节点这一段上
* Tips:lc25, lc206
*/
public
class
lc25
{
public
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
(
int
x
)
{
val
=
x
;
}
}
public
ListNode
reverseKGroup
(
ListNode
head
,
int
k
)
{
ListNode
curr
=
head
;
int
count
=
0
;
while
(
curr
!=
null
&&
count
!=
k
)
{
// 找下一段要反转的起始节点
curr
=
curr
.
next
;
count
++;
}
if
(
count
==
k
)
{
// 不足k的不执行
curr
=
reverseKGroup
(
curr
,
k
);
// 递归调用,反转下一段,并返回翻转后的头结点
// 反转当前段
while
(
count
--
>
0
)
{
// 链表反转的思路
ListNode
tmp
=
head
.
next
;
head
.
next
=
curr
;
curr
=
head
;
head
=
tmp
;
}
head
=
curr
;
}
return
head
;
}
}
code/lc300.java
浏览文件 @
abfdac66
...
...
@@ -14,7 +14,7 @@ public class lc300 {
if
(
nums
.
length
<
2
)
return
nums
.
length
;
int
[]
dp
=
new
int
[
nums
.
length
];
//dp[i] 存储以nums[i]结尾的最大长度
Arrays
.
fill
(
dp
,
1
);
Arrays
.
fill
(
dp
,
1
);
//记住fill 1
int
res
=
1
;
for
(
int
i
=
1
;
i
<
nums
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
i
;
j
++)
{
...
...
code/lc315.java
浏览文件 @
abfdac66
...
...
@@ -11,7 +11,7 @@ import java.util.List;
* 思路:两种思路,一种用二叉搜索树这类数据结构 https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST
* 一种归并排序的思路,归并的时候统计左右交换数目。如果一个数从这个数的右边交换到左边,则+1。因为有重复数字,所以用将index进行排序
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索
* 再有一种复杂度稍微高点的思路,从后往前插入排序,插入的时候二分搜索
,插入其实是不行的,因为插入操作以后还要移动value位置,又是一个O(N)的操作
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code
* Tips:好难呀,我日!
*/
...
...
code/lc72.java
浏览文件 @
abfdac66
...
...
@@ -9,7 +9,7 @@ package code;
*/
public
class
lc72
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
minDistance
(
"
intention"
,
"execution
"
));
System
.
out
.
println
(
minDistance
(
"
horse"
,
"ros
"
));
}
public
static
int
minDistance
(
String
word1
,
String
word2
)
{
int
[][]
dp
=
new
int
[
word1
.
length
()+
1
][
word2
.
length
()+
1
];
...
...
code/lc75.java
浏览文件 @
abfdac66
...
...
@@ -18,7 +18,7 @@ public class lc75 {
public
static
void
sortColors
(
int
[]
nums
)
{
int
l
=
0
;
int
r
=
nums
.
length
-
1
;
for
(
int
i
=
0
;
i
<=
r
;
i
++)
{
// i<r而不是length, 否则又换回来了
for
(
int
i
=
0
;
i
<=
r
;
i
++)
{
// i<
=
r而不是length, 否则又换回来了
if
(
nums
[
i
]==
0
&&
i
!=
l
){
//避免自己与自己交换
int
temp
=
nums
[
l
];
nums
[
l
]
=
nums
[
i
];
...
...
code/lc912.java
浏览文件 @
abfdac66
...
...
@@ -78,4 +78,43 @@ public class lc912 {
cur
++;
}
}
//堆排
public
int
[]
sortArray3
(
int
[]
nums
)
{
//建堆
int
pos
=
nums
.
length
/
2
;
while
(
pos
>=
0
){
AdjustTree
(
nums
,
nums
.
length
-
1
,
pos
);
pos
--;
}
//排序,每次找出最大的,从堆中去掉,调整堆
int
cur
=
nums
.
length
-
1
;
while
(
cur
>=
0
){
//交换位置
int
temp
=
nums
[
0
];
nums
[
0
]
=
nums
[
cur
];
nums
[
cur
]
=
temp
;
cur
--;
AdjustTree
(
nums
,
cur
,
0
);
}
return
nums
;
}
public
void
AdjustTree
(
int
[]
nums
,
int
len
,
int
pos
){
//调整堆
int
pos_exchange
=
pos
*
2
+
1
;
while
(
pos_exchange
<=
len
){
//left
if
(
pos_exchange
+
1
<=
len
&&
nums
[
pos_exchange
+
1
]>
nums
[
pos_exchange
]){
//比较左右节点,挑出来大的
pos_exchange
+=
1
;
}
if
(
nums
[
pos_exchange
]>
nums
[
pos
]){
//和父节点比较
int
temp
=
nums
[
pos
];
nums
[
pos
]
=
nums
[
pos_exchange
];
nums
[
pos_exchange
]
=
temp
;
pos
=
pos_exchange
;
pos_exchange
=
pos
*
2
+
1
;
}
else
{
break
;
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录