Skip to content

Commit 20c5d45

Browse files
committed
125
1 parent f637874 commit 20c5d45

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

SUMMARY.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
* [99. Recover Binary Search Tree](leetcode-99-Recover-Binary-Search-Tree.md)
103103
* [100. Same Tree](leetcode-100-Same-Tree.md)
104104
* [leetcode 100 斩!回顾](leetcode100斩回顾.md)
105-
* [101 题到 124](leetcode-101-200.md)
105+
* [101 题到 125](leetcode-101-200.md)
106106
* [101. Symmetric Tree](leetcode-101-Symmetric-Tree.md)
107107
* [102. Binary Tree Level Order Traversal](leetcode-102-Binary-Tree-Level-Order-Traversal.md)
108108
* [103. Binary Tree Zigzag Level Order Traversal](leetcode-103-Binary-Tree-Zigzag-Level-Order-Traversal.md)
@@ -126,4 +126,5 @@
126126
* [121. Best Time to Buy and Sell Stock](leetcode-121-Best-Time-to-Buy-and-Sell-Stock.md)
127127
* [122. Best Time to Buy and Sell Stock II](leetcode-122-Best-Time-to-Buy-and-Sell-StockII.md)
128128
* [123*. Best Time to Buy and Sell Stock III](leetcode-123-Best-Time-to-Buy-and-Sell-StockIII.md)
129-
* [124*. Binary Tree Maximum Path Sum](leetcode-124-Binary-Tree-Maximum-Path-Sum.md)
129+
* [124*. Binary Tree Maximum Path Sum](leetcode-124-Binary-Tree-Maximum-Path-Sum.md)
130+
* [125. Valid Palindrome](leetcode-125-Valid-Palindrome.md)

leetcode-101-200.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@
4444

4545
<a href="leetcode-123-Best-Time-to-Buy-and-Sell-StockIII.html">123. Best Time to Buy and Sell Stock III</a>
4646

47-
<a href="leetcode-124-Binary-Tree-Maximum-Path-Sum.html">124. Binary Tree Maximum Path Sum</a>
47+
<a href="leetcode-124-Binary-Tree-Maximum-Path-Sum.html">124. Binary Tree Maximum Path Sum</a>
48+
49+
<a href="leetcode-125-Valid-Palindrome.html">125. Valid Palindrome</a>

leetcode-125-Valid-Palindrome.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 题目描述(简单难度)
2+
3+
![](https://windliang.oss-cn-beijing.aliyuncs.com/125.jpg)
4+
5+
判断一个字符串是否是回文串,忽略掉除了字母和数字外的字符。
6+
7+
# 解法一
8+
9+
两个指针,一个指针从头进行,一个指针从尾部进行。依次判断两个指针的字符是否相等,同时要跳过非法字符。需要注意的是,两个指针不用从头到尾遍历,当两个指针相遇的时候就意味着这个字符串是回文串了。
10+
11+
还需要注意的是 `'A' == 'a'` ,也就是大写字母和小写字母是相同的。
12+
13+
```java
14+
public boolean isPalindrome(String s) {
15+
int len = s.length();
16+
s = s.toLowerCase(); //统一转为小写
17+
int i = 0, j = len - 1;
18+
while (i < j) {
19+
//跳过非法字符
20+
while (!isAlphanumeric(s.charAt(i))) {
21+
i++;
22+
//匹配 " " 这样的空白字符串防止越界
23+
if (i == len) {
24+
return true;
25+
}
26+
}
27+
while (!isAlphanumeric(s.charAt(j))) {
28+
j--;
29+
}
30+
if (s.charAt(i) != s.charAt(j)) {
31+
return false;
32+
}
33+
i++;
34+
j--;
35+
}
36+
return true;
37+
}
38+
39+
private boolean isAlphanumeric(char c) {
40+
if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9') {
41+
return true;
42+
}
43+
return false;
44+
}
45+
```
46+
47+
# 解法二
48+
49+
上边的是常规的思路,这里分享另一个 [思路](<https://leetcode.com/problems/valid-palindrome/discuss/39993/3ms-java-solution(beat-100-of-java-solution)>)
50+
51+
上边为了处理大小写字母的问题,首先全部统一为了小写。为了找出非法字符,每次都需要判断一下该字符是否在合法范围内。
52+
53+
这里用一个技巧,把 `'0'``'10'` 映射到 `1``10``'a'``'z'` 映射到 `11``36``'A'``'Z'` 也映射到 `11``36` 。然后把所有数字和字母用一个 `char` 数组存起来,没存的字符就默认映射到 `0` 了。
54+
55+
这样只需要判断字符串中每个字母映射过去的数字是否相等,如果是 `0` 就意味着它是非法字符。
56+
57+
```java
58+
private static final char[] charMap = new char[256];
59+
60+
static {
61+
// 映射 '0' 到 '9'
62+
for (int i = 0; i < 10; i++) {
63+
charMap[i + '0'] = (char) (1 + i); // numeric
64+
}
65+
// 映射 'a' 到 'z' 和 映射 'A' 到 'Z'
66+
for (int i = 0; i < 26; i++) {
67+
charMap[i + 'a'] = charMap[i + 'A'] = (char) (11 + i);
68+
}
69+
}
70+
71+
public boolean isPalindrome(String s) {
72+
char[] pChars = s.toCharArray();
73+
int start = 0, end = pChars.length - 1;
74+
char cS, cE;
75+
while (start < end) {
76+
// 得到映射后的数字
77+
cS = charMap[pChars[start]];
78+
cE = charMap[pChars[end]];
79+
if (cS != 0 && cE != 0) {
80+
if (cS != cE)
81+
return false;
82+
start++;
83+
end--;
84+
} else {
85+
if (cS == 0)
86+
start++;
87+
if (cE == 0)
88+
end--;
89+
}
90+
}
91+
return true;
92+
}
93+
```
94+
95+
#
96+
97+
很简单的一道题了,值得注意的就是解法二将所有字母进行映射,同时将大小写字母映射到同一个数字的想法,省了很多事,速度会提升一些。也可以做一下 [第 5 题](<https://leetcode.wang/leetCode-5-Longest-Palindromic-Substring.html>) ,给定一个字符串,找出最长的回文子串,里边的介绍的马拉车算法是真的太强了。

0 commit comments

Comments
 (0)