Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Coudy Hou
JavaGuide
提交
dc215294
J
JavaGuide
项目概览
Coudy Hou
/
JavaGuide
与 Fork 源项目一致
从无法访问的项目Fork
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JavaGuide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
dc215294
编写于
9月 20, 2018
作者:
S
SnailClimb
提交者:
GitHub
9月 20, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加目录
上级
36196cec
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
180 addition
and
164 deletion
+180
-164
数据结构与算法/搞定BAT面试——几道常见的子符串算法题.md
数据结构与算法/搞定BAT面试——几道常见的子符串算法题.md
+180
-164
未找到文件。
数据结构与算法/搞定BAT面试——几道常见的子符串算法题.md
浏览文件 @
dc215294
<!-- MarkdownTOC -->
-
[
说明
](
#说明
)
-
[
1. KMP 算法
](
#1-kmp-算法
)
-
[
2. 替换空格
](
#2-替换空格
)
-
[
3. 最长公共前缀
](
#3-最长公共前缀
)
-
[
4. 回文串
](
#4-回文串
)
-
[
4.1. 最长回文串
](
#41-最长回文串
)
-
[
4.2. 验证回文串
](
#42-验证回文串
)
-
[
4.3. 最长回文子串
](
#43-最长回文子串
)
-
[
4.4. 最长回文子序列
](
#44-最长回文子序列
)
-
[
5. 括号匹配深度
](
#5-括号匹配深度
)
-
[
6. 把字符串转换成整数
](
#6-把字符串转换成整数
)
<!-- /MarkdownTOC -->
## 说明
-
本文作者:wwwxmu
...
...
@@ -36,34 +53,34 @@
//https://www.weiweiblog.cn/replacespace/
public
class
Solution
{
/**
* 第一种方法:常规方法。利用String.charAt(i)以及String.valueOf(char).equals(" "
* )遍历字符串并判断元素是否为空格。是则替换为"%20",否则不替换
*/
public
static
String
replaceSpace
(
StringBuffer
str
)
{
int
length
=
str
.
length
();
// System.out.println("length=" + length);
StringBuffer
result
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
char
b
=
str
.
charAt
(
i
);
if
(
String
.
valueOf
(
b
).
equals
(
" "
))
{
result
.
append
(
"%20"
);
}
else
{
result
.
append
(
b
);
}
}
return
result
.
toString
();
}
/**
* 第二种方法:利用API替换掉所用空格,一行代码解决问题
*/
public
static
String
replaceSpace2
(
StringBuffer
str
)
{
return
str
.
toString
().
replaceAll
(
"\\s"
,
"%20"
);
}
/**
* 第一种方法:常规方法。利用String.charAt(i)以及String.valueOf(char).equals(" "
* )遍历字符串并判断元素是否为空格。是则替换为"%20",否则不替换
*/
public
static
String
replaceSpace
(
StringBuffer
str
)
{
int
length
=
str
.
length
();
// System.out.println("length=" + length);
StringBuffer
result
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
char
b
=
str
.
charAt
(
i
);
if
(
String
.
valueOf
(
b
).
equals
(
" "
))
{
result
.
append
(
"%20"
);
}
else
{
result
.
append
(
b
);
}
}
return
result
.
toString
();
}
/**
* 第二种方法:利用API替换掉所用空格,一行代码解决问题
*/
public
static
String
replaceSpace2
(
StringBuffer
str
)
{
return
str
.
toString
().
replaceAll
(
"\\s"
,
"%20"
);
}
}
```
...
...
@@ -93,36 +110,36 @@ public class Solution {
```
java
//https://leetcode-cn.com/problems/longest-common-prefix/description/
public
class
Main
{
public
static
String
replaceSpace
(
String
[]
strs
)
{
// 数组长度
int
len
=
strs
.
length
;
// 用于保存结果
StringBuffer
res
=
new
StringBuffer
();
// 注意:=是赋值,==是判断
if
(
strs
==
null
||
strs
.
length
==
0
)
{
return
""
;
}
// 给字符串数组的元素按照升序排序(包含数字的话,数字会排在前面)
Arrays
.
sort
(
strs
);
int
m
=
strs
[
0
].
length
();
int
n
=
strs
[
len
-
1
].
length
();
int
num
=
Math
.
min
(
m
,
n
);
for
(
int
i
=
0
;
i
<
num
;
i
++)
{
if
(
strs
[
0
].
charAt
(
i
)
==
strs
[
len
-
1
].
charAt
(
i
))
{
res
.
append
(
strs
[
0
].
charAt
(
i
));
}
else
break
;
}
return
res
.
toString
();
}
public
static
String
replaceSpace
(
String
[]
strs
)
{
// 数组长度
int
len
=
strs
.
length
;
// 用于保存结果
StringBuffer
res
=
new
StringBuffer
();
// 注意:=是赋值,==是判断
if
(
strs
==
null
||
strs
.
length
==
0
)
{
return
""
;
}
// 给字符串数组的元素按照升序排序(包含数字的话,数字会排在前面)
Arrays
.
sort
(
strs
);
int
m
=
strs
[
0
].
length
();
int
n
=
strs
[
len
-
1
].
length
();
int
num
=
Math
.
min
(
m
,
n
);
for
(
int
i
=
0
;
i
<
num
;
i
++)
{
if
(
strs
[
0
].
charAt
(
i
)
==
strs
[
len
-
1
].
charAt
(
i
))
{
res
.
append
(
strs
[
0
].
charAt
(
i
));
}
else
break
;
}
return
res
.
toString
();
}
//测试
public
static
void
main
(
String
[]
args
)
{
String
[]
strs
=
{
"customer"
,
"car"
,
"cat"
};
System
.
out
.
println
(
Main
.
replaceSpace
(
strs
));
//c
}
public
static
void
main
(
String
[]
args
)
{
String
[]
strs
=
{
"customer"
,
"car"
,
"cat"
};
System
.
out
.
println
(
Main
.
replaceSpace
(
strs
));
//c
}
}
```
...
...
@@ -161,23 +178,23 @@ public class Main {
```
java
//https://leetcode-cn.com/problems/longest-palindrome/description/
class
Solution
{
public
int
longestPalindrome
(
String
s
)
{
if
(
s
.
length
()
==
0
)
return
0
;
// 用于存放字符
HashSet
<
Character
>
hashset
=
new
HashSet
<
Character
>();
char
[]
chars
=
s
.
toCharArray
();
int
count
=
0
;
for
(
int
i
=
0
;
i
<
chars
.
length
;
i
++)
{
if
(!
hashset
.
contains
(
chars
[
i
]))
{
// 如果hashset没有该字符就保存进去
hashset
.
add
(
chars
[
i
]);
}
else
{
// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除
hashset
.
remove
(
chars
[
i
]);
count
++;
}
}
return
hashset
.
isEmpty
()
?
count
*
2
:
count
*
2
+
1
;
}
public
int
longestPalindrome
(
String
s
)
{
if
(
s
.
length
()
==
0
)
return
0
;
// 用于存放字符
HashSet
<
Character
>
hashset
=
new
HashSet
<
Character
>();
char
[]
chars
=
s
.
toCharArray
();
int
count
=
0
;
for
(
int
i
=
0
;
i
<
chars
.
length
;
i
++)
{
if
(!
hashset
.
contains
(
chars
[
i
]))
{
// 如果hashset没有该字符就保存进去
hashset
.
add
(
chars
[
i
]);
}
else
{
// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除
hashset
.
remove
(
chars
[
i
]);
count
++;
}
}
return
hashset
.
isEmpty
()
?
count
*
2
:
count
*
2
+
1
;
}
}
```
...
...
@@ -203,26 +220,26 @@ class Solution {
```
java
//https://leetcode-cn.com/problems/valid-palindrome/description/
class
Solution
{
public
boolean
isPalindrome
(
String
s
)
{
if
(
s
.
length
()
==
0
)
return
true
;
int
l
=
0
,
r
=
s
.
length
()
-
1
;
while
(
l
<
r
)
{
// 从头和尾开始向中间遍历
if
(!
Character
.
isLetterOrDigit
(
s
.
charAt
(
l
)))
{
// 字符不是字母和数字的情况
l
++;
}
else
if
(!
Character
.
isLetterOrDigit
(
s
.
charAt
(
r
)))
{
// 字符不是字母和数字的情况
r
--;
}
else
{
// 判断二者是否相等
if
(
Character
.
toLowerCase
(
s
.
charAt
(
l
))
!=
Character
.
toLowerCase
(
s
.
charAt
(
r
)))
return
false
;
l
++;
r
--;
}
}
return
true
;
}
public
boolean
isPalindrome
(
String
s
)
{
if
(
s
.
length
()
==
0
)
return
true
;
int
l
=
0
,
r
=
s
.
length
()
-
1
;
while
(
l
<
r
)
{
// 从头和尾开始向中间遍历
if
(!
Character
.
isLetterOrDigit
(
s
.
charAt
(
l
)))
{
// 字符不是字母和数字的情况
l
++;
}
else
if
(!
Character
.
isLetterOrDigit
(
s
.
charAt
(
r
)))
{
// 字符不是字母和数字的情况
r
--;
}
else
{
// 判断二者是否相等
if
(
Character
.
toLowerCase
(
s
.
charAt
(
l
))
!=
Character
.
toLowerCase
(
s
.
charAt
(
r
)))
return
false
;
l
++;
r
--;
}
}
return
true
;
}
}
```
...
...
@@ -254,28 +271,28 @@ class Solution {
```
java
//https://leetcode-cn.com/problems/longest-palindromic-substring/description/
class
Solution
{
private
int
index
,
len
;
public
String
longestPalindrome
(
String
s
)
{
if
(
s
.
length
()
<
2
)
return
s
;
for
(
int
i
=
0
;
i
<
s
.
length
()
-
1
;
i
++)
{
PalindromeHelper
(
s
,
i
,
i
);
PalindromeHelper
(
s
,
i
,
i
+
1
);
}
return
s
.
substring
(
index
,
index
+
len
);
}
public
void
PalindromeHelper
(
String
s
,
int
l
,
int
r
)
{
while
(
l
>=
0
&&
r
<
s
.
length
()
&&
s
.
charAt
(
l
)
==
s
.
charAt
(
r
))
{
l
--;
r
++;
}
if
(
len
<
r
-
l
-
1
)
{
index
=
l
+
1
;
len
=
r
-
l
-
1
;
}
}
private
int
index
,
len
;
public
String
longestPalindrome
(
String
s
)
{
if
(
s
.
length
()
<
2
)
return
s
;
for
(
int
i
=
0
;
i
<
s
.
length
()
-
1
;
i
++)
{
PalindromeHelper
(
s
,
i
,
i
);
PalindromeHelper
(
s
,
i
,
i
+
1
);
}
return
s
.
substring
(
index
,
index
+
len
);
}
public
void
PalindromeHelper
(
String
s
,
int
l
,
int
r
)
{
while
(
l
>=
0
&&
r
<
s
.
length
()
&&
s
.
charAt
(
l
)
==
s
.
charAt
(
r
))
{
l
--;
r
++;
}
if
(
len
<
r
-
l
-
1
)
{
index
=
l
+
1
;
len
=
r
-
l
-
1
;
}
}
}
```
...
...
@@ -381,20 +398,20 @@ import java.util.Scanner;
* @Description: TODO 求给定合法括号序列的深度
*/
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
String
s
=
sc
.
nextLine
();
int
cnt
=
0
,
max
=
0
,
i
;
for
(
i
=
0
;
i
<
s
.
length
();
++
i
)
{
if
(
s
.
charAt
(
i
)
==
'('
)
cnt
++;
else
cnt
--;
max
=
Math
.
max
(
max
,
cnt
);
}
sc
.
close
();
System
.
out
.
println
(
max
);
}
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
String
s
=
sc
.
nextLine
();
int
cnt
=
0
,
max
=
0
,
i
;
for
(
i
=
0
;
i
<
s
.
length
();
++
i
)
{
if
(
s
.
charAt
(
i
)
==
'('
)
cnt
++;
else
cnt
--;
max
=
Math
.
max
(
max
,
cnt
);
}
sc
.
close
();
System
.
out
.
println
(
max
);
}
}
```
...
...
@@ -407,39 +424,39 @@ public class Main {
//https://www.weiweiblog.cn/strtoint/
public
class
Main
{
public
static
int
StrToInt
(
String
str
)
{
if
(
str
.
length
()
==
0
)
return
0
;
char
[]
chars
=
str
.
toCharArray
();
// 判断是否存在符号位
int
flag
=
0
;
if
(
chars
[
0
]
==
'+'
)
flag
=
1
;
else
if
(
chars
[
0
]
==
'-'
)
flag
=
2
;
int
start
=
flag
>
0
?
1
:
0
;
int
res
=
0
;
// 保存结果
for
(
int
i
=
start
;
i
<
chars
.
length
;
i
++)
{
if
(
Character
.
isDigit
(
chars
[
i
]))
{
// 调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False
int
temp
=
chars
[
i
]
-
'0'
;
res
=
res
*
10
+
temp
;
}
else
{
return
0
;
}
}
return
flag
==
1
?
res
:
-
res
;
}
public
static
void
main
(
String
[]
args
)
{
// TODO Auto-generated method stub
String
s
=
"-12312312"
;
System
.
out
.
println
(
"使用库函数转换:"
+
Integer
.
valueOf
(
s
));
int
res
=
Main
.
StrToInt
(
s
);
System
.
out
.
println
(
"使用自己写的方法转换:"
+
res
);
}
public
static
int
StrToInt
(
String
str
)
{
if
(
str
.
length
()
==
0
)
return
0
;
char
[]
chars
=
str
.
toCharArray
();
// 判断是否存在符号位
int
flag
=
0
;
if
(
chars
[
0
]
==
'+'
)
flag
=
1
;
else
if
(
chars
[
0
]
==
'-'
)
flag
=
2
;
int
start
=
flag
>
0
?
1
:
0
;
int
res
=
0
;
// 保存结果
for
(
int
i
=
start
;
i
<
chars
.
length
;
i
++)
{
if
(
Character
.
isDigit
(
chars
[
i
]))
{
// 调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False
int
temp
=
chars
[
i
]
-
'0'
;
res
=
res
*
10
+
temp
;
}
else
{
return
0
;
}
}
return
flag
==
1
?
res
:
-
res
;
}
public
static
void
main
(
String
[]
args
)
{
// TODO Auto-generated method stub
String
s
=
"-12312312"
;
System
.
out
.
println
(
"使用库函数转换:"
+
Integer
.
valueOf
(
s
));
int
res
=
Main
.
StrToInt
(
s
);
System
.
out
.
println
(
"使用自己写的方法转换:"
+
res
);
}
}
```
\ No newline at end of file
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录