File tree Expand file tree Collapse file tree 8 files changed +153
-39
lines changed
0100-0199/0125.Valid Palindrome Expand file tree Collapse file tree 8 files changed +153
-39
lines changed Original file line number Diff line number Diff line change 2222<strong >输出:</strong > false
2323</pre >
2424
25-
2625## 解法
2726
2827<!-- 这里可写通用的实现逻辑 -->
3433<!-- 这里可写当前语言的特殊实现逻辑 -->
3534
3635``` 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
3850```
3951
4052### ** Java**
4153
4254<!-- 这里可写当前语言的特殊实现逻辑 -->
4355
4456``` 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+ ```
4576
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+ };
46104```
47105
48106### **...**
Original file line number Diff line number Diff line change 3131 <li><code>s</code> consists only of printable ASCII characters.</li>
3232</ul >
3333
34-
3534## Solutions
3635
3736<!-- tabs:start -->
3837
3938### ** Python3**
4039
4140``` 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
4355```
4456
4557### ** Java**
4658
4759``` 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+ ```
4879
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+ };
49107```
50108
51109### **...**
Original file line number Diff line number Diff line change 11class Solution {
22public:
33 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 ;
99 else {
10- ++left ;
11- --right ;
10+ ++i ;
11+ --j ;
1212 }
1313 }
1414 return true ;
Original file line number Diff line number Diff line change 11class Solution {
22 public boolean isPalindrome (String s ) {
3- int i = 0 ;
4- int j = s .length () - 1 ;
3+ int i = 0 , j = s .length () - 1 ;
54 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 ))) {
1310 return false ;
11+ } else {
12+ ++i ;
13+ --j ;
1414 }
15- i ++;
16- j --;
1715 }
1816 return true ;
1917 }
You can’t perform that action at this time.
0 commit comments