Skip to content

feat: add solutions to lcof2 problem: No.018 #1475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions lcof2/剑指 Offer II 018. 有效的回文/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@

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

**方法一:双指针**

我们定义两个指针 $i$ 和 $j$,初始时分别指向字符串的首尾位置,每次判断两个指针指向的字符是否为数字或字母,如果两个指针指向的字符都为数字或字母时,判断两个指针指向的字符是否相同(忽略大小写),如果不相同则返回 `false`,否则将两个指针向中间移动一位,直到两个指针相遇时返回 `true`。

时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -58,8 +64,7 @@ class Solution:
j -= 1
if s[i].lower() != s[j].lower():
return False
i += 1
j -= 1
i, j = i + 1, j - 1
return True
```

Expand All @@ -73,22 +78,47 @@ class Solution {
int i = 0, j = s.length() - 1;
while (i < j) {
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
i++;
++i;
}
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
j--;
--j;
}
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
return false;
}
i++;
j--;
++i;
--j;
}
return true;
}
}
```

### **C++**

```cpp
class Solution {
public:
bool isPalindrome(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
while (i < j && !isalnum(s[i])) {
++i;
}
while (i < j && !isalnum(s[j])) {
--j;
}
if (tolower(s[i]) != tolower(s[j])) {
return false;
}
++i;
--j;
}
return true;
}
};
```

### **Go**

```go
Expand All @@ -104,8 +134,7 @@ func isPalindrome(s string) bool {
if tolower(s[i]) != tolower(s[j]) {
return false
}
i++
j--
i, j = i+1, j-1
}
return true
}
Expand Down
20 changes: 20 additions & 0 deletions lcof2/剑指 Offer II 018. 有效的回文/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool isPalindrome(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
while (i < j && !isalnum(s[i])) {
++i;
}
while (i < j && !isalnum(s[j])) {
--j;
}
if (tolower(s[i]) != tolower(s[j])) {
return false;
}
++i;
--j;
}
return true;
}
};
3 changes: 1 addition & 2 deletions lcof2/剑指 Offer II 018. 有效的回文/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ func isPalindrome(s string) bool {
if tolower(s[i]) != tolower(s[j]) {
return false
}
i++
j--
i, j = i+1, j-1
}
return true
}
Expand Down
38 changes: 19 additions & 19 deletions lcof2/剑指 Offer II 018. 有效的回文/Solution.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class Solution {
public boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
i++;
}
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
j--;
}
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
return false;
}
i++;
j--;
}
return true;
}
}
class Solution {
public boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
++i;
}
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
--j;
}
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
return false;
}
++i;
--j;
}
return true;
}
}
25 changes: 12 additions & 13 deletions lcof2/剑指 Offer II 018. 有效的回文/Solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
while i < j and not s[i].isalnum():
i += 1
while i < j and not s[j].isalnum():
j -= 1
if s[i].lower() != s[j].lower():
return False
i += 1
j -= 1
return True
class Solution:
def isPalindrome(self, s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
while i < j and not s[i].isalnum():
i += 1
while i < j and not s[j].isalnum():
j -= 1
if s[i].lower() != s[j].lower():
return False
i, j = i + 1, j - 1
return True