-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString125.java
More file actions
77 lines (76 loc) · 1.99 KB
/
String125.java
File metadata and controls
77 lines (76 loc) · 1.99 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package string;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String125
* @Author: markey
* @Description:
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
*
* 说明:本题中,我们将空字符串定义为有效的回文串。
*
* 示例 1:
*
* 输入: "A man, a plan, a canal: Panama"
* 输出: true
* 示例 2:
*
* 输入: "race a car"
* 输出: false
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/valid-palindrome
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/2/8 18:46
* @Version: 1.0
*/
public class String125 {
public static void main(String[] args) {
isPalindrome("A man, a plan, a canal: Panama");
}
public static boolean isPalindrome(String s) {
int start = 0, end = s.length() - 1;
while (start < end) {
if (!isValid(s.charAt(start))) {
start++;
continue;
}
if (!isValid(s.charAt(end))) {
end--;
continue;
}
if (equals(s.charAt(start), s.charAt(end))) {
start++;
end--;
} else {
System.out.println(start + " " + end);
return false;
}
}
return true;
}
private static boolean equals(char a, char b) {
if (a == b) {
return true;
}
if (a <= 'z' && a >= 'a') {
return a - b == 32;
}
if (a <= 'Z' && a >= 'A') {
return b - a == 32;
}
return false;
}
private static boolean isValid(char c) {
if (c <= '9' && c >= '0') {
return true;
}
if (c <= 'z' && c >= 'a') {
return true;
}
if (c <= 'Z' && c >= 'A') {
return true;
}
return false;
}
}