avatar

day19_125_验证回文串

题目

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

1
2
输入: "A man, a plan, a canal: Panama"
输出: true

示例 2:

1
2
输入: "race a car"
输出: false

Related Topics

  • 双指针
  • 字符串

解答

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
45
46
47
48
49
50
51
52
53
54
//给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。 
//
// 说明:本题中,我们将空字符串定义为有效的回文串。
//
// 示例 1:
//
// 输入: "A man, a plan, a canal: Panama"
//输出: true
//
//
// 示例 2:
//
// 输入: "race a car"
//输出: false
//
// Related Topics 双指针 字符串


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isPalindrome(String s) {
if (s.length() == 0)
return true;

String low = s.toLowerCase();

int i = 0;
int j = low.length() - 1;

while (i < j)
{
if (!Character.isLetterOrDigit(low.charAt(i)))
{
i++;
continue;
}
if (!Character.isLetterOrDigit(low.charAt(j)))
{
j--;
continue;
}
if (low.charAt(i) != low.charAt(j))
return false;
else
{
i++;
j--;
}
}
return true;
}

}
//leetcode submit region end(Prohibit modification and deletion)
文章作者: 无知的小狼
文章链接: https://bytedance.press/2020/05/13/20200501/day19_125_%E9%AA%8C%E8%AF%81%E5%9B%9E%E6%96%87%E4%B8%B2/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 无知的小狼

评论