-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution006.java
More file actions
40 lines (33 loc) · 869 Bytes
/
Solution006.java
File metadata and controls
40 lines (33 loc) · 869 Bytes
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
package algorithm.tmop;
/**
* @author: mayuan
* @desc: 回文判断
* 时间复杂度: O(n)
* 空间复杂度: O(1)
* @date:
*/
public class Solution006 {
public static void main(String[] args) {
final String text1 = "abcd";
final String text2 = "abcdcba";
final String text3 = "123454321";
System.out.println(isPalindrome(text1));
System.out.println(isPalindrome(text2));
System.out.println(isPalindrome(text3));
}
public static boolean isPalindrome(String str) {
if (null == str) {
return false;
}
int start = 0;
int end = str.length() - 1;
while (start < end) {
if (str.charAt(start) != str.charAt(end)) {
return false;
}
++start;
--end;
}
return true;
}
}