Skip to content

Commit a4dacea

Browse files
authored
feat: add solutions to lcof2 problem: No.018 (#1475)
No.018.验证回文串II
1 parent bb9b599 commit a4dacea

File tree

5 files changed

+89
-42
lines changed

5 files changed

+89
-42
lines changed

lcof2/剑指 Offer II 018. 有效的回文/README.md

+37-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:双指针**
45+
46+
我们定义两个指针 $i$ 和 $j$,初始时分别指向字符串的首尾位置,每次判断两个指针指向的字符是否为数字或字母,如果两个指针指向的字符都为数字或字母时,判断两个指针指向的字符是否相同(忽略大小写),如果不相同则返回 `false`,否则将两个指针向中间移动一位,直到两个指针相遇时返回 `true`
47+
48+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
49+
4450
<!-- tabs:start -->
4551

4652
### **Python3**
@@ -58,8 +64,7 @@ class Solution:
5864
j -= 1
5965
if s[i].lower() != s[j].lower():
6066
return False
61-
i += 1
62-
j -= 1
67+
i, j = i + 1, j - 1
6368
return True
6469
```
6570

@@ -73,22 +78,47 @@ class Solution {
7378
int i = 0, j = s.length() - 1;
7479
while (i < j) {
7580
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
76-
i++;
81+
++i;
7782
}
7883
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
79-
j--;
84+
--j;
8085
}
8186
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
8287
return false;
8388
}
84-
i++;
85-
j--;
89+
++i;
90+
--j;
8691
}
8792
return true;
8893
}
8994
}
9095
```
9196

97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool isPalindrome(string s) {
103+
int i = 0, j = s.size() - 1;
104+
while (i < j) {
105+
while (i < j && !isalnum(s[i])) {
106+
++i;
107+
}
108+
while (i < j && !isalnum(s[j])) {
109+
--j;
110+
}
111+
if (tolower(s[i]) != tolower(s[j])) {
112+
return false;
113+
}
114+
++i;
115+
--j;
116+
}
117+
return true;
118+
}
119+
};
120+
```
121+
92122
### **Go**
93123
94124
```go
@@ -104,8 +134,7 @@ func isPalindrome(s string) bool {
104134
if tolower(s[i]) != tolower(s[j]) {
105135
return false
106136
}
107-
i++
108-
j--
137+
i, j = i+1, j-1
109138
}
110139
return true
111140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(string s) {
4+
int i = 0, j = s.size() - 1;
5+
while (i < j) {
6+
while (i < j && !isalnum(s[i])) {
7+
++i;
8+
}
9+
while (i < j && !isalnum(s[j])) {
10+
--j;
11+
}
12+
if (tolower(s[i]) != tolower(s[j])) {
13+
return false;
14+
}
15+
++i;
16+
--j;
17+
}
18+
return true;
19+
}
20+
};

lcof2/剑指 Offer II 018. 有效的回文/Solution.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ func isPalindrome(s string) bool {
1010
if tolower(s[i]) != tolower(s[j]) {
1111
return false
1212
}
13-
i++
14-
j--
13+
i, j = i+1, j-1
1514
}
1615
return true
1716
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
class Solution {
2-
public boolean isPalindrome(String s) {
3-
int i = 0, j = s.length() - 1;
4-
while (i < j) {
5-
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
6-
i++;
7-
}
8-
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
9-
j--;
10-
}
11-
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
12-
return false;
13-
}
14-
i++;
15-
j--;
16-
}
17-
return true;
18-
}
19-
}
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
int i = 0, j = s.length() - 1;
4+
while (i < j) {
5+
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
6+
++i;
7+
}
8+
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
9+
--j;
10+
}
11+
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
12+
return false;
13+
}
14+
++i;
15+
--j;
16+
}
17+
return true;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
class Solution:
2-
def isPalindrome(self, s: str) -> bool:
3-
i, j = 0, len(s) - 1
4-
while i < j:
5-
while i < j and not s[i].isalnum():
6-
i += 1
7-
while i < j and not s[j].isalnum():
8-
j -= 1
9-
if s[i].lower() != s[j].lower():
10-
return False
11-
i += 1
12-
j -= 1
13-
return True
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
i, j = 0, len(s) - 1
4+
while i < j:
5+
while i < j and not s[i].isalnum():
6+
i += 1
7+
while i < j and not s[j].isalnum():
8+
j -= 1
9+
if s[i].lower() != s[j].lower():
10+
return False
11+
i, j = i + 1, j - 1
12+
return True

0 commit comments

Comments
 (0)