Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
42 lines (38 loc) · 1.09 KB

File metadata and controls

42 lines (38 loc) · 1.09 KB
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
package leetcode.string;
/**
* 验证回文字符串
*
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
*
* 说明:本题中,我们将空字符串定义为有效的回文串。
*
* 示例 1:
* 输入: "A man, a plan, a canal: Panama"
* 输出: true
*
* 示例 2:
* 输入: "race a car"
* 输出: false
*/
public class IsPalindrome {
public static void main(String[] args) {
String s = "A man, a plan, a canal: Panama";
System.out.println(isPalindrome(s));
}
public static boolean isPalindrome(String s) {
s = s.toLowerCase();
StringBuilder l = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if ((s.charAt(i) >= '0' && s.charAt(i) <= '9') || (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')) {
l.append(s.charAt(i));
}
}
int j = 0, k = l.length() - 1;
while (j < k) {
if (l.charAt(j++) != l.charAt(k--)) {
return false;
}
}
return true;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.