Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0125
Browse files Browse the repository at this point in the history
No.0125.Valid Palindrome
  • Loading branch information
yanglbme committed Apr 20, 2023
1 parent 220616d commit 1140934
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 304 deletions.
200 changes: 78 additions & 122 deletions solution/0100-0199/0125.Valid Palindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@

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

**方法一:双指针**

我们用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 的两端,接下来循环以下过程,直至 $i \geq j$:

1. 如果 $s[i]$ 不是字母或数字,指针 $i$ 右移一位,继续下一次循环;
1. 如果 $s[j]$ 不是字母或数字,指针 $j$ 左移一位,继续下一次循环;
1. 如果 $s[i]$ 和 $s[j]$ 的小写形式不相等,返回 `false`
1. 否则,指针 $i$ 右移一位,指针 $j$ 左移一位,继续下一次循环。

循环结束,返回 `true`

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

<!-- tabs:start -->

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

Expand All @@ -88,7 +100,7 @@ class Solution {
++i;
} else if (!Character.isLetterOrDigit(s.charAt(j))) {
--j;
} else if (Character.toUpperCase(s.charAt(i)) != Character.toUpperCase(s.charAt(j))) {
} else if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
return false;
} else {
++i;
Expand All @@ -108,43 +120,67 @@ public:
bool isPalindrome(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
if (!isAlphaNum(s[i]))
if (!isalnum(s[i])) {
++i;
else if (!isAlphaNum(s[j]))
} else if (!isalnum(s[j])) {
--j;
else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32)
} else if (tolower(s[i]) != tolower(s[j])) {
return false;
else {
} else {
++i;
--j;
}
}
return true;
}

private:
bool isAlphaNum(char& ch) {
if (ch >= 'a' && ch <= 'z') return true;
if (ch >= 'A' && ch <= 'Z') return true;
if (ch >= '0' && ch <= '9') return true;
return false;
}
};
```
### **Go**
```go
func isPalindrome(s string) bool {
i, j := 0, len(s)-1
for i < j {
if !isalnum(s[i]) {
i++
} else if !isalnum(s[j]) {
j--
} else if tolower(s[i]) != tolower(s[j]) {
return false
} else {
i, j = i+1, j-1
}
}
return true
}
func isalnum(ch byte) bool {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
}
func tolower(ch byte) byte {
if ch >= 'A' && ch <= 'Z' {
return ch + 32
}
return ch
}
```

### **C#**

```cs
using System.Linq;
public class Solution {
public bool IsPalindrome(string s) {
var chars = s.Where(ch => char.IsLetterOrDigit(ch)).Select(char.ToLower).ToList();
var i = 0;
var j = chars.Count - 1;
for (; i < j; ++i, --j)
{
if (chars[i] != chars[j]) return false;
int i = 0, j = s.Length - 1;
while (i < j) {
if (!char.IsLetterOrDigit(s[i])) {
++i;
} else if (!char.IsLetterOrDigit(s[j])) {
--j;
} else if (char.ToLower(s[i++]) != char.ToLower(s[j--])) {
return false;
}
}
return true;
}
Expand All @@ -159,57 +195,18 @@ public class Solution {
* @return {boolean}
*/
var isPalindrome = function (s) {
let arr1 = [],
arr2 = [];
for (let i = 0; i < s.length; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
arr1.push(s[i].toLowerCase());
}
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z')) {
arr1.push(s[i]);
}
}
arr2 = [...arr1];
arr2.reverse();
return arr1.join('') === arr2.join('');
};
```

```js
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
function isNumOrAl(a) {
if (
(a >= 'A' && a <= 'Z') ||
(a >= '0' && a <= '9') ||
(a >= 'a' && a <= 'z')
) {
return true;
} else {
return false;
}
}

if (s.length === 0) {
return true;
}
let i = 0,
j = s.length - 1;
let i = 0;
let j = s.length - 1;
while (i < j) {
while (i < j && !isNumOrAl(s[i])) {
i++;
}
while (i < j && !isNumOrAl(s[j])) {
j--;
}
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
if (!/[a-zA-Z0-9]/.test(s[i])) {
++i;
} else if (!/[a-zA-Z0-9]/.test(s[j])) {
--j;
} else if (s[i].toLowerCase() !== s[j].toLowerCase()) {
return false;
} else {
i++;
j--;
++i;
--j;
}
}
return true;
Expand All @@ -220,36 +217,24 @@ var isPalindrome = function (s) {

```ts
function isPalindrome(s: string): boolean {
let left: number = 0,
right: number = s.length - 1;
while (left < right) {
let char1: string = s.charAt(left);
let char2: string = s.charAt(right);
if (!/[a-zA-Z0-9]/.test(char1)) {
++left;
} else if (!/[a-zA-Z0-9]/.test(char2)) {
--right;
} else if (char1.toLocaleLowerCase() != char2.toLocaleLowerCase()) {
let i = 0;
let j = s.length - 1;
while (i < j) {
if (!/[a-zA-Z0-9]/.test(s[i])) {
++i;
} else if (!/[a-zA-Z0-9]/.test(s[j])) {
--j;
} else if (s[i].toLowerCase() !== s[j].toLowerCase()) {
return false;
} else {
++left;
--right;
++i;
--j;
}
}
return true;
}
```

```ts
function isPalindrome(s: string): boolean {
const isAlphanumeric = (c: string) => {
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
};
const cs = s.toLocaleLowerCase().split('').filter(isAlphanumeric);
return cs.join('') === cs.reverse().join('');
}
```

### **Rust**

```rust
Expand Down Expand Up @@ -279,35 +264,6 @@ impl Solution {
}
```

### **Go**

```go
func isPalindrome(s string) bool {
s = strings.ToLower(s)
left, right := 0, len(s) - 1
for left < right {
for left < right && !verify(s[left]) {
left++
}
for left < right && !verify(s[right]) {
right--
}
if left < right {
if s[left] != s[right] {
return false
}
left++
right--
}
}
return true
}

func verify(ch byte) bool {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
}
```

### **PHP**

```php
Expand Down
Loading

0 comments on commit 1140934

Please sign in to comment.