-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution125.java
More file actions
57 lines (52 loc) · 1.4 KB
/
Copy pathSolution125.java
File metadata and controls
57 lines (52 loc) · 1.4 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
/**
* @Title: Solution125.java——
* @Package EasyCode
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月22日 上午11:18:42
* @version V1.0
*/
package EasyCode;
import java.util.Stack;
/**
* @ClassName: Solution125——验证回文串 不考空格,只考虑子母和符号
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月22日 上午11:18:42
*/
public class Solution125 {
//思路没对...重来
/* public static boolean isPalindrome(String s) {
s = s.toLowerCase().replaceAll(" ", "");
Stack<Character> stack = new Stack<>();
if(s.length() % 2 != 0)
return false;
for(int i = 0 ; i < s.length(); i++){
if(i < s.length() / 2)
stack.push(s.charAt(i));
else {
if(!stack.pop().equals(s.charAt(i)))
return false;
}
}
return true;
}*/
public static boolean isPalindrome(String s) {
if (s == null) return true;
s = s.toLowerCase();
int l = s.length();
StringBuilder str = new StringBuilder(l);
for (char c : s.toCharArray()) {
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
str.append(c);
}
}
return str.toString().equals(str.reverse().toString());
}
public static void main(String[] args) {
String string = "race a car";
// string = string.replaceAll(" ", "");
// System.out.println(string);
System.out.println(isPalindrome(string));
}
}