Skip to content

Commit 06a602d

Browse files
committed
feat: add solutions to lcof2 problem: No.018
ๅ‰‘ๆŒ‡ Offer II 018. ๆœ‰ๆ•ˆ็š„ๅ›žๆ–‡
1 parent 9df6ef4 commit 06a602d

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

โ€Žlcof2/ๅ‰‘ๆŒ‡ Offer II 018. ๆœ‰ๆ•ˆ็š„ๅ›žๆ–‡/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,56 @@ func isalnum(b byte) bool {
124124
}
125125
```
126126

127+
### **TypeScript**
128+
129+
```ts
130+
function isPalindrome(s: string): boolean {
131+
const str = s.replace(/[^a-zA-Z0-9]/g, '');
132+
let l = 0;
133+
let r = str.length - 1;
134+
while (l < r) {
135+
if (str[l].toLocaleLowerCase() !== str[r].toLocaleLowerCase()) {
136+
return false;
137+
}
138+
l++;
139+
r--;
140+
}
141+
return true;
142+
}
143+
```
144+
145+
### **Rust**
146+
147+
ไฝฟ็”จ `is_alphabetic()` ไธŽ `is_numeric()` ่ฟ‡ๆปคๅญ—็ฌฆ
148+
149+
```rust
150+
impl Solution {
151+
pub fn is_palindrome(s: String) -> bool {
152+
let ss: Vec<char> = s.chars().collect();
153+
let mut l = 0;
154+
let mut r = ss.len() - 1;
155+
while l < r {
156+
while l < r && !(ss[l].is_alphabetic() || ss[l].is_numeric()) {
157+
l += 1;
158+
}
159+
while l < r && !(ss[r].is_alphabetic() || ss[r].is_numeric()) {
160+
r -= 1;
161+
}
162+
if ss[l].to_ascii_lowercase() != ss[r].to_ascii_lowercase() {
163+
return false;
164+
}
165+
// ้˜ฒๆญข usize ็ ด็•Œ
166+
if r == 0 {
167+
return true;
168+
}
169+
l += 1;
170+
r -= 1;
171+
}
172+
true
173+
}
174+
}
175+
```
176+
127177
### **...**
128178

129179
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
pub fn is_palindrome(s: String) -> bool {
3+
let ss: Vec<char> = s.chars().collect();
4+
let mut l = 0;
5+
let mut r = ss.len() - 1;
6+
while l < r {
7+
while l < r && !(ss[l].is_alphabetic() || ss[l].is_numeric()) {
8+
l += 1;
9+
}
10+
while l < r && !(ss[r].is_alphabetic() || ss[r].is_numeric()) {
11+
r -= 1;
12+
}
13+
if ss[l].to_ascii_lowercase() != ss[r].to_ascii_lowercase() {
14+
return false;
15+
}
16+
// ้˜ฒๆญข usize ็ ด็•Œ
17+
if r == 0 {
18+
return true;
19+
}
20+
l += 1;
21+
r -= 1;
22+
}
23+
true
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isPalindrome(s: string): boolean {
2+
const str = s.replace(/[^a-zA-Z0-9]/g, '');
3+
let l = 0;
4+
let r = str.length - 1;
5+
while (l < r) {
6+
if (str[l].toLocaleLowerCase() !== str[r].toLocaleLowerCase()) {
7+
return false;
8+
}
9+
l++;
10+
r--;
11+
}
12+
return true;
13+
}

0 commit comments

Comments
ย (0)