FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc125.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc125.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (42 loc) · 1.51 KB
Breadcrumbs
leetcode
/
code
/
lc125.java
Copy path
File metadata and controls
44 lines (42 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package
code
;
/*
* 125. Valid Palindrome
* 题意:判断是否为回文
* 难度:Easy
* 分类:Two Pointers, String
* 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。
* Tips:记下另一种方法。第一种方法Bingo!
* lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc125
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
isPalindrome
(
"A man, a plan, a canal: Panama"
));
}
public
static
boolean
isPalindrome
(
String
s
) {
int
begin
=
0
;
int
end
=
s
.
length
()-
1
;
s
=
s
.
toLowerCase
();
//转为小写
while
(
begin
<
end
&& !(
helper
(
s
.
charAt
(
begin
))))
begin
++;
while
(
begin
<
end
&& !(
helper
(
s
.
charAt
(
end
))))
end
--;
while
(
begin
<
end
){
if
(
s
.
charAt
(
begin
)==
s
.
charAt
(
end
)){
begin
++;
end
--;
while
(
begin
<
end
&& !(
helper
(
s
.
charAt
(
begin
))))
begin
++;
while
(
begin
<
end
&& !(
helper
(
s
.
charAt
(
end
))))
end
--;
}
else
{
return
false
;
}
}
return
true
;
}
public
static
boolean
helper
(
Character
ch
){
//过滤非字符数字
if
(
Character
.
isAlphabetic
(
ch
)||
Character
.
isDigit
(
ch
))
return
true
;
return
false
;
}
public
boolean
isPalindrome2
(
String
s
) {
String
actual
=
s
.
replaceAll
(
"[^A-Za-z0-9]"
,
""
).
toLowerCase
();
String
rev
=
new
StringBuffer
(
actual
).
reverse
().
toString
();
return
actual
.
equals
(
rev
);
}
}
Back
|
FazBrowse Home
|
New Git URL