Skip to content

Commit 87fb929

Browse files
committedAug 23, 2021
feat: add solutions to lcof2 problems: No.018,019
1 parent d1a3103 commit 87fb929

File tree

8 files changed

+266
-2
lines changed

8 files changed

+266
-2
lines changed
 

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,80 @@
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```python
52-
52+
class Solution:
53+
def isPalindrome(self, s: str) -> bool:
54+
i, j = 0, len(s) - 1
55+
while i < j:
56+
while i < j and not s[i].isalnum():
57+
i += 1
58+
while i < j and not s[j].isalnum():
59+
j -= 1
60+
if s[i].lower() != s[j].lower():
61+
return False
62+
i += 1
63+
j -= 1
64+
return True
5365
```
5466

5567
### **Java**
5668

5769
<!-- 这里可写当前语言的特殊实现逻辑 -->
5870

5971
```java
72+
class Solution {
73+
public boolean isPalindrome(String s) {
74+
int i = 0, j = s.length() - 1;
75+
while (i < j) {
76+
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
77+
i++;
78+
}
79+
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
80+
j--;
81+
}
82+
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
83+
return false;
84+
}
85+
i++;
86+
j--;
87+
}
88+
return true;
89+
}
90+
}
91+
```
6092

93+
### **Go**
94+
95+
```go
96+
func isPalindrome(s string) bool {
97+
i, j := 0, len(s)-1
98+
for i < j {
99+
for i < j && !isalnum(s[i]) {
100+
i++
101+
}
102+
for i < j && !isalnum(s[j]) {
103+
j--
104+
}
105+
if tolower(s[i]) != tolower(s[j]) {
106+
return false
107+
}
108+
i++
109+
j--
110+
}
111+
return true
112+
}
113+
114+
func tolower(b byte) byte {
115+
if b >= 'A' && b <= 'Z' {
116+
return b - 'A' + 'a'
117+
}
118+
return b
119+
}
120+
121+
func isalnum(b byte) bool {
122+
return b >= '0' && b <= '9' ||
123+
b >= 'a' && b <= 'z' ||
124+
b >= 'A' && b <= 'Z'
125+
}
61126
```
62127

63128
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func isPalindrome(s string) bool {
2+
i, j := 0, len(s)-1
3+
for i < j {
4+
for i < j && !isalnum(s[i]) {
5+
i++
6+
}
7+
for i < j && !isalnum(s[j]) {
8+
j--
9+
}
10+
if tolower(s[i]) != tolower(s[j]) {
11+
return false
12+
}
13+
i++
14+
j--
15+
}
16+
return true
17+
}
18+
19+
func tolower(b byte) byte {
20+
if b >= 'A' && b <= 'Z' {
21+
return b - 'A' + 'a'
22+
}
23+
return b
24+
}
25+
26+
func isalnum(b byte) bool {
27+
return b >= '0' && b <= '9' ||
28+
b >= 'a' && b <= 'z' ||
29+
b >= 'A' && b <= 'Z'
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

‎lcof2/剑指 Offer II 019. 最多删除一个字符得到回文/README.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,93 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
双指针,当 `s[i]` 不等于 `s[j]` 时,分别尝试跳过 `i` 或跳过 `j`
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

5456
<!-- 这里可写当前语言的特殊实现逻辑 -->
5557

5658
```python
57-
59+
class Solution:
60+
def validPalindrome(self, s: str) -> bool:
61+
def check(i, j):
62+
while i < j:
63+
if s[i] != s[j]:
64+
return False
65+
i += 1
66+
j -= 1
67+
return True
68+
69+
i, j = 0, len(s) - 1
70+
while i < j:
71+
if s[i] == s[j]:
72+
i += 1
73+
j -= 1
74+
else:
75+
return check(i + 1, j) or check(i, j - 1)
76+
return True
5877
```
5978

6079
### **Java**
6180

6281
<!-- 这里可写当前语言的特殊实现逻辑 -->
6382

6483
```java
84+
class Solution {
85+
public boolean validPalindrome(String s) {
86+
int i = 0, j = s.length() - 1;
87+
while (i < j) {
88+
if (s.charAt(i) == s.charAt(j)) {
89+
i++;
90+
j--;
91+
} else {
92+
return check(s, i + 1, j) || check(s, i, j - 1);
93+
}
94+
}
95+
return true;
96+
}
97+
98+
private boolean check(String s, int i, int j) {
99+
while (i < j) {
100+
if (s.charAt(i) != s.charAt(j)) {
101+
return false;
102+
}
103+
i++;
104+
j--;
105+
}
106+
return true;
107+
}
108+
}
109+
```
65110

111+
### **Go**
112+
113+
```go
114+
func validPalindrome(s string) bool {
115+
i, j := 0, len(s)-1
116+
for i < j {
117+
if s[i] == s[j] {
118+
i++
119+
j--
120+
} else {
121+
return check(s, i+1, j) || check(s, i, j-1)
122+
}
123+
}
124+
return true
125+
}
126+
127+
func check(s string, i, j int) bool {
128+
for i < j {
129+
if s[i] != s[j] {
130+
return false
131+
}
132+
i++
133+
j--
134+
}
135+
return true
136+
}
66137
```
67138

68139
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func validPalindrome(s string) bool {
2+
i, j := 0, len(s)-1
3+
for i < j {
4+
if s[i] == s[j] {
5+
i++
6+
j--
7+
} else {
8+
return check(s, i+1, j) || check(s, i, j-1)
9+
}
10+
}
11+
return true
12+
}
13+
14+
func check(s string, i, j int) bool {
15+
for i < j {
16+
if s[i] != s[j] {
17+
return false
18+
}
19+
i++
20+
j--
21+
}
22+
return true
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean validPalindrome(String s) {
3+
int i = 0, j = s.length() - 1;
4+
while (i < j) {
5+
if (s.charAt(i) == s.charAt(j)) {
6+
i++;
7+
j--;
8+
} else {
9+
return check(s, i + 1, j) || check(s, i, j - 1);
10+
}
11+
}
12+
return true;
13+
}
14+
15+
private boolean check(String s, int i, int j) {
16+
while (i < j) {
17+
if (s.charAt(i) != s.charAt(j)) {
18+
return false;
19+
}
20+
i++;
21+
j--;
22+
}
23+
return true;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def validPalindrome(self, s: str) -> bool:
3+
def check(i, j):
4+
while i < j:
5+
if s[i] != s[j]:
6+
return False
7+
i += 1
8+
j -= 1
9+
return True
10+
11+
i, j = 0, len(s) - 1
12+
while i < j:
13+
if s[i] == s[j]:
14+
i += 1
15+
j -= 1
16+
else:
17+
return check(i + 1, j) or check(i, j - 1)
18+
return True

0 commit comments

Comments
 (0)