File tree 8 files changed +153
-39
lines changed
0100-0199/0125.Valid Palindrome
8 files changed +153
-39
lines changed Original file line number Diff line number Diff line change 22
22
<strong >输出:</strong > false
23
23
</pre >
24
24
25
-
26
25
## 解法
27
26
28
27
<!-- 这里可写通用的实现逻辑 -->
34
33
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
34
36
35
``` python
37
-
36
+ class Solution :
37
+ def isPalindrome (self , s : str ) -> bool :
38
+ i, j = 0 , len (s) - 1
39
+ while i < j:
40
+ if not s[i].isalnum():
41
+ i += 1
42
+ elif not s[j].isalnum():
43
+ j -= 1
44
+ elif s[i].lower() != s[j].lower():
45
+ return False
46
+ else :
47
+ i += 1
48
+ j -= 1
49
+ return True
38
50
```
39
51
40
52
### ** Java**
41
53
42
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
55
44
56
``` java
57
+ class Solution {
58
+ public boolean isPalindrome (String s ) {
59
+ int i = 0 , j = s. length() - 1 ;
60
+ while (i < j) {
61
+ if (! Character . isLetterOrDigit(s. charAt(i))) {
62
+ ++ i;
63
+ } else if (! Character . isLetterOrDigit(s. charAt(j))) {
64
+ -- j;
65
+ } else if (Character . toUpperCase(s. charAt(i)) != Character . toUpperCase(s. charAt(j))) {
66
+ return false ;
67
+ } else {
68
+ ++ i;
69
+ -- j;
70
+ }
71
+ }
72
+ return true ;
73
+ }
74
+ }
75
+ ```
45
76
77
+ ### ** C++**
78
+
79
+ ``` cpp
80
+ class Solution {
81
+ public:
82
+ bool isPalindrome(string s) {
83
+ int i = 0, j = s.size() - 1;
84
+ while (i < j) {
85
+ if (!isAlphaNum(s[ i] )) ++i;
86
+ else if (!isAlphaNum(s[ j] )) --j;
87
+ else if ((s[ i] + 32 - 'a') % 32 != (s[ j] + 32 - 'a') % 32) return false;
88
+ else {
89
+ ++i;
90
+ --j;
91
+ }
92
+ }
93
+ return true;
94
+ }
95
+
96
+ private:
97
+ bool isAlphaNum(char &ch) {
98
+ if (ch >= 'a' && ch <= 'z') return true;
99
+ if (ch >= 'A' && ch <= 'Z') return true;
100
+ if (ch >= '0' && ch <= '9') return true;
101
+ return false;
102
+ }
103
+ };
46
104
```
47
105
48
106
### **...**
Original file line number Diff line number Diff line change 31
31
<li><code>s</code> consists only of printable ASCII characters.</li>
32
32
</ul >
33
33
34
-
35
34
## Solutions
36
35
37
36
<!-- tabs:start -->
38
37
39
38
### ** Python3**
40
39
41
40
``` python
42
-
41
+ class Solution :
42
+ def isPalindrome (self , s : str ) -> bool :
43
+ i, j = 0 , len (s) - 1
44
+ while i < j:
45
+ if not s[i].isalnum():
46
+ i += 1
47
+ elif not s[j].isalnum():
48
+ j -= 1
49
+ elif s[i].lower() != s[j].lower():
50
+ return False
51
+ else :
52
+ i += 1
53
+ j -= 1
54
+ return True
43
55
```
44
56
45
57
### ** Java**
46
58
47
59
``` java
60
+ class Solution {
61
+ public boolean isPalindrome (String s ) {
62
+ int i = 0 , j = s. length() - 1 ;
63
+ while (i < j) {
64
+ if (! Character . isLetterOrDigit(s. charAt(i))) {
65
+ ++ i;
66
+ } else if (! Character . isLetterOrDigit(s. charAt(j))) {
67
+ -- j;
68
+ } else if (Character . toUpperCase(s. charAt(i)) != Character . toUpperCase(s. charAt(j))) {
69
+ return false ;
70
+ } else {
71
+ ++ i;
72
+ -- j;
73
+ }
74
+ }
75
+ return true ;
76
+ }
77
+ }
78
+ ```
48
79
80
+ ### ** C++**
81
+
82
+ ``` cpp
83
+ class Solution {
84
+ public:
85
+ bool isPalindrome(string s) {
86
+ int i = 0, j = s.size() - 1;
87
+ while (i < j) {
88
+ if (!isAlphaNum(s[ i] )) ++i;
89
+ else if (!isAlphaNum(s[ j] )) --j;
90
+ else if ((s[ i] + 32 - 'a') % 32 != (s[ j] + 32 - 'a') % 32) return false;
91
+ else {
92
+ ++i;
93
+ --j;
94
+ }
95
+ }
96
+ return true;
97
+ }
98
+
99
+ private:
100
+ bool isAlphaNum(char &ch) {
101
+ if (ch >= 'a' && ch <= 'z') return true;
102
+ if (ch >= 'A' && ch <= 'Z') return true;
103
+ if (ch >= '0' && ch <= '9') return true;
104
+ return false;
105
+ }
106
+ };
49
107
```
50
108
51
109
### **...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
bool isPalindrome (string s) {
4
- int left = 0 , right = s.size () - 1 ;
5
- while (left < right ) {
6
- if (!isAlphaNum (s[left ])) ++left ;
7
- else if (!isAlphaNum (s[right ])) --right ;
8
- else if ((s[left ] + 32 - ' a' ) % 32 != (s[right ] + 32 - ' a' ) % 32 ) return false ;
4
+ int i = 0 , j = s.size () - 1 ;
5
+ while (i < j ) {
6
+ if (!isAlphaNum (s[i ])) ++i ;
7
+ else if (!isAlphaNum (s[j ])) --j ;
8
+ else if ((s[i ] + 32 - ' a' ) % 32 != (s[j ] + 32 - ' a' ) % 32 ) return false ;
9
9
else {
10
- ++left ;
11
- --right ;
10
+ ++i ;
11
+ --j ;
12
12
}
13
13
}
14
14
return true ;
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean isPalindrome (String s ) {
3
- int i = 0 ;
4
- int j = s .length () - 1 ;
3
+ int i = 0 , j = s .length () - 1 ;
5
4
while (i < j ) {
6
- while (i < j && !Character .isLetterOrDigit (s .charAt (i ))) {
7
- i ++;
8
- }
9
- while (i < j && !Character .isLetterOrDigit (s .charAt (j ))) {
10
- j --;
11
- }
12
- if (i < j && Character .toUpperCase (s .charAt (i )) != Character .toUpperCase (s .charAt (j ))) {
5
+ if (!Character .isLetterOrDigit (s .charAt (i ))) {
6
+ ++i ;
7
+ } else if (!Character .isLetterOrDigit (s .charAt (j ))) {
8
+ --j ;
9
+ } else if (Character .toUpperCase (s .charAt (i )) != Character .toUpperCase (s .charAt (j ))) {
13
10
return false ;
11
+ } else {
12
+ ++i ;
13
+ --j ;
14
14
}
15
- i ++;
16
- j --;
17
15
}
18
16
return true ;
19
17
}
You can’t perform that action at this time.
0 commit comments