Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
awesome-algorithm
提交
d3d58c6b
A
awesome-algorithm
项目概览
OpenDocCN
/
awesome-algorithm
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
awesome-algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d3d58c6b
编写于
5月 05, 2019
作者:
K
Keqi Huang
提交者:
GitHub
5月 05, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #260 from ruanwenjun/leetcode-java
Leetcode java
上级
3522a0a2
4ee41884
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
165 addition
and
0 deletion
+165
-0
docs/Leetcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md
...tcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md
+87
-0
docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring.md
...ode_Solutions/Java/0005._Longest_Palindromic_Substring.md
+78
-0
未找到文件。
docs/Leetcode_Solutions/Java/0004._Median_of_Two_Sorted_Arrays.md
0 → 100644
浏览文件 @
d3d58c6b
# 4. Median of Two Sorted Arrays
**<font color=red>难度: Hard</font>**
## 刷题内容
> 原题连接
*
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description
> 内容描述
```
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
```
## 解题方案
> 思路 1
******- 时间复杂度: O(log(m + n))******
- 空间复杂度: O(1)
****
**
将问题转化为两个有序数组,找出其中的第K大的数, beats 99.80%
```
java
class
Solution
{
// 寻找两个有序数组的中位数
// 问题转换为求第K大的数
public
double
findMedianSortedArrays
(
int
[]
nums1
,
int
[]
nums2
)
{
if
(
nums1
==
null
||
nums1
.
length
==
0
){
// 求nums2的中位数
return
nums2
.
length
%
2
==
0
?
(
nums2
[
nums2
.
length
/
2
]
+
nums2
[
nums2
.
length
/
2
-
1
])
/
2.0
:
nums2
[
nums2
.
length
/
2
];
}
if
(
nums2
==
null
||
nums2
.
length
==
0
){
return
nums1
.
length
%
2
==
0
?
(
nums1
[
nums1
.
length
/
2
]
+
nums1
[
nums1
.
length
/
2
-
1
])
/
2.0
:
nums1
[
nums1
.
length
/
2
];
}
int
len
=
nums1
.
length
+
nums2
.
length
;
return
len
%
2
==
0
?
(
topK
(
nums1
,
nums2
,
0
,
0
,
len
/
2
)+
topK
(
nums1
,
nums2
,
0
,
0
,
len
/
2
+
1
))/
2.0
:
topK
(
nums1
,
nums2
,
0
,
0
,
len
/
2
+
1
);
}
// 找两个有序数组的topk小的数
public
int
topK
(
int
[]
nums1
,
int
[]
nums2
,
int
start1
,
int
start2
,
int
k
){
if
(
start1
>=
nums1
.
length
){
return
nums2
[
start2
+
k
-
1
];
}
if
(
start2
>=
nums2
.
length
){
return
nums1
[
start1
+
k
-
1
];
}
if
(
k
==
1
){
return
Math
.
min
(
nums1
[
start1
]
,
nums2
[
start2
]);
}
if
(
start1
+
k
/
2
>
nums1
.
length
){
// 肯定不会在nums2的前 k / 2
return
topK
(
nums1
,
nums2
,
start1
,
start2
+
k
/
2
,
k
-
k
/
2
);
}
else
if
(
start2
+
k
/
2
>
nums2
.
length
){
return
topK
(
nums1
,
nums2
,
start1
+
k
/
2
,
start2
,
k
-
k
/
2
);
}
int
mid1
=
nums1
[
start1
+
k
/
2
-
1
];
int
mid2
=
nums2
[
start2
+
k
/
2
-
1
];
if
(
mid1
>
mid2
){
// 移除nums2的前k/2
return
topK
(
nums1
,
nums2
,
start1
,
start2
+
k
/
2
,
k
-
k
/
2
);
}
else
{
return
topK
(
nums1
,
nums2
,
start1
+
k
/
2
,
start2
,
k
-
k
/
2
);
}
}
}
```
docs/Leetcode_Solutions/Java/0005._Longest_Palindromic_Substring.md
0 → 100644
浏览文件 @
d3d58c6b
# 5. Longest Palindromic Substring
**<font color=red>难度: Medium</font>**
## 刷题内容
> 原题连接
*
https://leetcode-cn.com/problems/longest-palindromic-substring/description
> 内容描述
```
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
```
## 解题方案
> 思路 1
******- 时间复杂度: O(n^2)******
- 空间复杂度: O(n^2)
****
**
使用动态规划的思路,用一个二维数组cache
[
i
][
j
]
记录i到j是否为回文串, beats 54.36%
```
java
class
Solution
{
// 采用动态规划
// 如果 i == j ,则只有一个字母 肯定是回文串
// 如果 char[i] == char[j] && j == i + 1 , 两个字母相等 肯定是回文串
// 如果 char[i] == char[j] && j > i + 1 && cache[i+1][j-1]为true,则肯定是回文串
public
String
longestPalindrome
(
String
s
)
{
if
(
s
==
null
||
s
.
length
()
<
2
){
return
s
;
}
boolean
[][]
cache
=
new
boolean
[
s
.
length
()][
s
.
length
()];
// 记录 i ~ j 是否是回文串
char
[]
chars
=
s
.
toCharArray
();
int
len
=
s
.
length
();
int
start
=
0
;
int
end
=
0
;
// 采用至底向上的动态规划,也可以采用递归方式
for
(
int
i
=
len
-
1
;
i
>=
0
;
i
--){
for
(
int
j
=
i
;
j
<
len
;
j
++){
if
(
i
==
j
){
cache
[
i
][
j
]
=
true
;
}
else
if
(
j
==
i
+
1
&&
chars
[
i
]
==
chars
[
j
]){
cache
[
i
][
j
]
=
true
;
if
(
end
-
start
+
1
<
2
){
end
=
j
;
start
=
i
;
}
}
else
if
(
chars
[
i
]
==
chars
[
j
]
&&
cache
[
i
+
1
][
j
-
1
]){
cache
[
i
][
j
]
=
true
;
if
(
end
-
start
<
j
-
i
){
start
=
i
;
end
=
j
;
}
}
else
{
cache
[
i
][
j
]
=
false
;
}
}
}
return
s
.
substring
(
start
,
end
+
1
);
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录