Skip to content

Commit ec46f81

Browse files
committed
string
1 parent 03a54ec commit ec46f81

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/string/test5/Solution.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @Description: 最长回文子串:
2121
* 1-动态规划;时间复杂度:O(n2),空间复杂度:O(n2)
2222
* 2-中心扩展算法;时间复杂度:O(n2),空间复杂度:O(1)
23+
* 3-判断字符串是否是回文串;时间复杂度:O(n),空间复杂度:O(1)
2324
*/
2425
public class Solution {
2526

@@ -92,12 +93,40 @@ private int expandAroundCenter(String s, int left, int right) {
9293
return right - left - 1;
9394
}
9495

96+
/**
97+
* 3-判断字符串是否是回文串
98+
*
99+
* @param s
100+
* @return
101+
*/
102+
public boolean isPalindrome(String s) {
103+
if (s == null || s.length() == 0) {
104+
return false;
105+
}
106+
107+
int len = s.length();
108+
int j = len - 1;
109+
for (int i = 0; i < len / 2; i++) {
110+
char head = s.charAt(i);
111+
char tail = s.charAt(j);
112+
if (head != tail) {
113+
return false;
114+
}
115+
j--;
116+
}
117+
return true;
118+
}
119+
95120
@Test
96121
public void test() {
97122
System.out.println("123456".substring(0, 3));
98123
System.out.println(longestPalindrome("dcacdefd"));
99124
System.out.println(longestPalindrome("babad"));
100125
System.out.println(longestPalindrome("cbbd"));
126+
System.out.println("---------------");
127+
System.out.println(isPalindrome("12345"));
128+
System.out.println(isPalindrome("12321"));
129+
System.out.println(isPalindrome("1221"));
101130
}
102131

103132
}

0 commit comments

Comments
 (0)