Skip to content

Commit 90b14e4

Browse files
committed
add 125
1 parent b94b78b commit 90b14e4

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
| 7 | [Reverse Integer][007] |
3636
| 387 | [First Unique Character in a String][387] |
3737
| 242 | [Valid Anagram][242] |
38+
| 125 | [Valid Palindrome][125] |
39+
3840

3941
**其他**
4042

@@ -64,3 +66,4 @@
6466
[007]: https://github.com/andavid/leetcode-java/blob/master/note/007/README.md
6567
[387]: https://github.com/andavid/leetcode-java/blob/master/note/387/README.md
6668
[242]: https://github.com/andavid/leetcode-java/blob/master/note/242/README.md
69+
[125]: https://github.com/andavid/leetcode-java/blob/master/note/125/README.md

note/125/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [Valid Palindrome][title]
2+
3+
## Description
4+
5+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
6+
7+
For example,
8+
"A man, a plan, a canal: Panama" is a palindrome.
9+
"race a car" is not a palindrome.
10+
11+
**Note:**
12+
Have you consider that the string might be empty? This is a good question to ask during an interview.
13+
14+
For the purpose of this problem, we define empty string as valid palindrome.
15+
16+
## 思路
17+
18+
判断字符串是否是回文字符串,忽略除了数字和大小写字母之外的字符。
19+
使用头和尾两个指针往中间扫描,遇到非数字和字母的字符跳过,比较两个字符是否相等(忽略大小写)。
20+
21+
## [完整代码][src]
22+
23+
```java
24+
class Solution {
25+
public boolean isPalindrome(String s) {
26+
if (s == null || s.length() <= 1) {
27+
return true;
28+
}
29+
30+
int i = 0;
31+
int j = s.length() - 1;
32+
33+
while (i < j) {
34+
char lc = s.charAt(i);
35+
char rc = s.charAt(j);
36+
37+
if (!isAlphaNumeric(lc)) {
38+
i++;
39+
continue;
40+
}
41+
42+
if (!isAlphaNumeric(rc)) {
43+
j--;
44+
continue;
45+
}
46+
47+
if (toLowerCase(lc) != toLowerCase(rc)) {
48+
return false;
49+
}
50+
51+
i++;
52+
j--;
53+
}
54+
55+
return true;
56+
}
57+
58+
public boolean isAlphaNumeric(char c) {
59+
return (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
60+
}
61+
62+
public int toLowerCase(char c) {
63+
return (c >= 97 && c <= 122) ? c - 32 : c;
64+
}
65+
}
66+
```
67+
68+
[title]: https://leetcode.com/problems/valid-palindrome
69+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_125/Solution.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
if (s == null || s.length() <= 1) {
4+
return true;
5+
}
6+
7+
int i = 0;
8+
int j = s.length() - 1;
9+
10+
while (i < j) {
11+
char lc = s.charAt(i);
12+
char rc = s.charAt(j);
13+
14+
if (!isAlphaNumeric(lc)) {
15+
i++;
16+
continue;
17+
}
18+
19+
if (!isAlphaNumeric(rc)) {
20+
j--;
21+
continue;
22+
}
23+
24+
if (toLowerCase(lc) != toLowerCase(rc)) {
25+
return false;
26+
}
27+
28+
i++;
29+
j--;
30+
}
31+
32+
return true;
33+
}
34+
35+
public boolean isAlphaNumeric(char c) {
36+
return (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
37+
}
38+
39+
public int toLowerCase(char c) {
40+
return (c >= 97 && c <= 122) ? c - 32 : c;
41+
}
42+
43+
public static void main(String[] args) {
44+
Solution solution = new Solution();
45+
System.out.println(solution.isPalindrome("A man, a plan, a canal: Panama"));
46+
System.out.println(solution.isPalindrome("0P"));
47+
}
48+
}

0 commit comments

Comments
 (0)