File tree 5 files changed +89
-42
lines changed
lcof2/剑指 Offer II 018. 有效的回文
5 files changed +89
-42
lines changed Original file line number Diff line number Diff line change 41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
+ ** 方法一:双指针**
45
+
46
+ 我们定义两个指针 $i$ 和 $j$,初始时分别指向字符串的首尾位置,每次判断两个指针指向的字符是否为数字或字母,如果两个指针指向的字符都为数字或字母时,判断两个指针指向的字符是否相同(忽略大小写),如果不相同则返回 ` false ` ,否则将两个指针向中间移动一位,直到两个指针相遇时返回 ` true ` 。
47
+
48
+ 时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
49
+
44
50
<!-- tabs:start -->
45
51
46
52
### ** Python3**
@@ -58,8 +64,7 @@ class Solution:
58
64
j -= 1
59
65
if s[i].lower() != s[j].lower():
60
66
return False
61
- i += 1
62
- j -= 1
67
+ i, j = i + 1 , j - 1
63
68
return True
64
69
```
65
70
@@ -73,22 +78,47 @@ class Solution {
73
78
int i = 0 , j = s. length() - 1 ;
74
79
while (i < j) {
75
80
while (i < j && ! Character . isLetterOrDigit(s. charAt(i))) {
76
- i ++ ;
81
+ ++ i ;
77
82
}
78
83
while (i < j && ! Character . isLetterOrDigit(s. charAt(j))) {
79
- j -- ;
84
+ -- j ;
80
85
}
81
86
if (Character . toLowerCase(s. charAt(i)) != Character . toLowerCase(s. charAt(j))) {
82
87
return false ;
83
88
}
84
- i ++ ;
85
- j -- ;
89
+ ++ i ;
90
+ -- j ;
86
91
}
87
92
return true ;
88
93
}
89
94
}
90
95
```
91
96
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
+
92
122
### **Go**
93
123
94
124
```go
@@ -104,8 +134,7 @@ func isPalindrome(s string) bool {
104
134
if tolower(s[i]) != tolower(s[j]) {
105
135
return false
106
136
}
107
- i++
108
- j--
137
+ i, j = i+1, j-1
109
138
}
110
139
return true
111
140
}
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -10,8 +10,7 @@ func isPalindrome(s string) bool {
10
10
if tolower (s [i ]) != tolower (s [j ]) {
11
11
return false
12
12
}
13
- i ++
14
- j --
13
+ i , j = i + 1 , j - 1
15
14
}
16
15
return true
17
16
}
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments