Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
墨明棋妙_1
leetcode
提交
1bb9af43
L
leetcode
项目概览
墨明棋妙_1
/
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 搜索 >>
提交
1bb9af43
编写于
12月 26, 2018
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20181225
上级
dad7d566
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
200 addition
and
6 deletion
+200
-6
code/lc23.java
code/lc23.java
+47
-6
code/lc31.java
code/lc31.java
+52
-0
code/lc32.java
code/lc32.java
+58
-0
code/lc33.java
code/lc33.java
+43
-0
未找到文件。
code/lc23.java
浏览文件 @
1bb9af43
package
code
;
import
java.util.Comparator
;
import
java.util.List
;
import
java.util.PriorityQueue
;
/*
* 2
2. Generate Parenthese
s
* 题意:
正确括号组合的
* 难度:
Medium
* 分类:
String, Backtracking
* 思路:
回溯法的典型题目,按选优条件向前搜索,达到目标后就退回一步或返回
* 注意:
递归法别忘了两块的拼接,例如n=4时,可以由2,2拼起来作为答案
* 2
3. Merge k Sorted List
s
* 题意:
K个有序链表合并
* 难度:
Hard
* 分类:
Linked List, Divide and Conquer, Heap
* 思路:
优先队列或分治方法
* 注意:
优先队列如何定义比较方法
*/
public
class
lc23
{
public
class
ListNode
{
...
...
@@ -45,4 +46,44 @@ public class lc23 {
}
return
head
.
next
;
}
public
ListNode
mergeKLists2
(
ListNode
[]
lists
)
{
if
(
lists
==
null
||
lists
.
length
==
0
)
return
null
;
return
Partition
(
lists
,
0
,
lists
.
length
-
1
);
}
public
ListNode
Partition
(
ListNode
[]
lists
,
int
start
,
int
end
){
if
(
end
-
start
==
0
)
return
lists
[
end
];
else
{
int
mid
=
(
start
+
end
)/
2
;
ListNode
l1
=
Partition
(
lists
,
start
,
mid
);
ListNode
l2
=
Partition
(
lists
,
mid
+
1
,
end
);
return
Merge2List
(
l1
,
l2
);
}
}
public
ListNode
Merge2List
(
ListNode
ln1
,
ListNode
ln2
){
ListNode
res
=
new
ListNode
(
0
);
ListNode
head
=
res
;
while
(
ln1
!=
null
&&
ln2
!=
null
){
if
(
ln1
.
val
<
ln2
.
val
){
head
.
next
=
ln1
;
head
=
head
.
next
;
ln1
=
ln1
.
next
;
}
else
{
head
.
next
=
ln2
;
head
=
head
.
next
;
ln2
=
ln2
.
next
;
}
}
if
(
ln1
!=
null
){
head
.
next
=
ln1
;
}
if
(
ln2
!=
null
){
head
.
next
=
ln2
;
}
return
res
.
next
;
}
}
code/lc31.java
0 → 100644
浏览文件 @
1bb9af43
package
code
;
/*
* 31. Next Permutation
* 题意:找出排列组合的下一个排列
* 难度:Medium
* 分类:Array
* 思路:从后往前找第一个变小的数x,从这个数之后的数中找出第一个比x大的数,交换,再把之后的数逆序即可
* Tips:很典型的排列组合题,思路方法记忆一下。注意比较时是否有=。
*/
public
class
lc31
{
public
static
void
main
(
String
[]
args
)
{
int
[]
nums
=
{
2
,
3
,
1
,
3
,
3
};
nextPermutation
(
nums
);
for
(
int
i:
nums
){
System
.
out
.
println
(
i
);
}
}
public
static
void
nextPermutation
(
int
[]
nums
)
{
int
ptr
=
nums
.
length
-
1
;
//从后往前找第一个变小的数x
while
(
ptr
>
0
&&
nums
[
ptr
-
1
]>=
nums
[
ptr
]){
// 注意是 >= {5,1,1} , 等于--
ptr
--;
}
if
(
ptr
!=
0
){
//从这个数之后的数中找出第一个比x大的数
int
n
=
nums
[
ptr
];
int
ptr2
=
ptr
;
for
(
int
i
=
ptr
+
1
;
i
<
nums
.
length
;
i
++){
if
(
nums
[
i
]>
nums
[
ptr
-
1
]
&&
nums
[
i
]<=
n
)
{
//注意 <= {2,3,1,3,3}
n
=
nums
[
i
];
ptr2
=
i
;
}
}
nums
[
ptr2
]
=
nums
[
ptr
-
1
];
nums
[
ptr
-
1
]
=
n
;
}
//把之后的数逆序
ReverseNums
(
nums
,
ptr
,
nums
.
length
-
1
);
}
public
static
void
ReverseNums
(
int
[]
nums
,
int
start
,
int
end
){
int
l
=
end
+
start
;
for
(
int
i
=
start
;
i
<
(
start
+
end
+
1
)/
2
;
i
++)
{
int
temp
=
nums
[
i
];
nums
[
i
]
=
nums
[
l
-
i
];
nums
[
l
-
i
]
=
temp
;
}
}
}
code/lc32.java
0 → 100644
浏览文件 @
1bb9af43
package
code
;
import
java.util.Stack
;
/*
* 31. Longest Valid Parentheses
* 题意:最长有效子串
* 难度:Hard
* 分类:Dynamic Programming, String
* 思路:两种常规方法,一是dp,每个位置记录以该位置结尾的最长长度。另一种是用栈,把位置索引入栈。
* Tips:想到了用dp,也想到了用数组记录位置结尾的解,但没有想好如何进行更新迭代计算。把位置索引入栈的方法很典型,关注一下。
*/
public
class
lc32
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
longestValidParentheses2
(
"()(())"
));
}
public
static
int
longestValidParentheses
(
String
s
)
{
if
(
s
.
length
()==
0
)
return
0
;
// dp 方法
int
[]
dp
=
new
int
[
s
.
length
()+
1
];
int
res
=
0
;
for
(
int
i
=
2
;
i
<
dp
.
length
;
i
++)
{
if
(
s
.
charAt
(
i
-
1
)==
'('
)
dp
[
i
]
=
0
;
else
{
if
(
s
.
charAt
(
i
-
2
)==
'('
){
// 这种情况:(())()
dp
[
i
]
=
dp
[
i
-
2
]+
2
;
}
else
if
(
i
-
2
-
dp
[
i
-
1
]>=
0
&&
s
.
charAt
(
i
-
2
-
dp
[
i
-
1
])==
'('
){
// 这种情况:()(())
dp
[
i
]
=
dp
[
i
-
1
]
+
dp
[
i
-
2
-
dp
[
i
-
1
]]+
2
;
}
}
if
(
dp
[
i
]>
res
)
res
=
dp
[
i
];
}
return
res
;
}
public
static
int
longestValidParentheses2
(
String
s
)
{
//栈方法 ()(())
Stack
<
Integer
>
st
=
new
Stack
();
st
.
add
(-
1
);
int
res
=
0
;
for
(
int
i
=
0
;
i
<
s
.
length
()
;
i
++)
{
char
ch
=
s
.
charAt
(
i
);
if
(
ch
==
'('
)
st
.
add
(
i
);
else
if
(
ch
==
')'
){
st
.
pop
();
if
(
st
.
isEmpty
())
st
.
push
(
i
);
res
=
Math
.
max
(
res
,
i
-
st
.
peek
());
}
}
return
res
;
}
}
code/lc33.java
0 → 100644
浏览文件 @
1bb9af43
package
code
;
/*
* 31. Search in Rotated Sorted Array
* 题意:Longest Valid Parentheses
* 难度:Hard
* 分类:Array, Binary Search
* 思路:二分查找的思路,多了一步判断,判断哪部分有序,是否在这部分中
* Tips:注意边界判断,是否有等号
*/
public
class
lc33
{
public
static
void
main
(
String
[]
args
)
{
int
[]
nums
=
{
1
,
3
};
System
.
out
.
println
(
search
(
nums
,
3
));
}
public
static
int
search
(
int
[]
nums
,
int
target
)
{
int
start
=
0
;
int
end
=
nums
.
length
-
1
;
while
(
start
<=
end
){
if
(
start
==
end
){
if
(
nums
[
start
]==
target
)
return
start
;
return
-
1
;
}
int
mid
=
(
start
+
end
)/
2
;
if
(
target
==
nums
[
mid
])
return
mid
;
if
(
nums
[
mid
]>
nums
[
end
]){
//左边是有序的
if
(
nums
[
start
]<=
target
&&
target
<=
nums
[
mid
])
end
=
mid
-
1
;
else
start
=
mid
+
1
;
}
else
{
//右边是有序的
if
(
nums
[
mid
]<=
target
&&
target
<=
nums
[
end
])
start
=
mid
+
1
;
else
end
=
mid
-
1
;
}
}
return
-
1
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录