Skip to content

feat: add solutions to lcof2 problem: No.019 #1508

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 1 commit into from
Aug 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@

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

双指针,当 `s[i]` 不等于 `s[j]` 时,分别尝试跳过 `i` 或跳过 `j`。
**方法一:双指针**

我们用两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 的第一个字符和最后一个字符,然后向中间移动指针,每次判断 $s[i]$ 和 $s[j]$ 是否相等:

- 如果 $s[i] = s[j]$,则指针 $i$ 向后移动一位,指针 $j$ 向前移动一位;
- 否则,存在两种情况,即删除字符 $s[i]$ 或者删除字符 $s[j]$,然后判断删除之后的字符串是否是回文字符串。即判断子串 $s[i+1..j]$ 或者子串 $s[i..j-1]$ 是否是回文字符串。

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

<!-- tabs:start -->

Expand All @@ -57,7 +64,7 @@
```python
class Solution:
def validPalindrome(self, s: str) -> bool:
def check(i, j):
def check(i: int, j: int) -> bool:
while i < j:
if s[i] != s[j]:
return False
Expand All @@ -67,7 +74,7 @@ class Solution:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return check(i, j - 1) or check(i + 1, j)
return check(i + 1, j) or check(i, j - 1)
i, j = i + 1, j - 1
return True
```
Expand All @@ -78,16 +85,19 @@ class Solution:

```java
class Solution {
private String s;

public boolean validPalindrome(String s) {
this.s = s;
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return check(s, i + 1, j) || check(s, i, j - 1);
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}

private boolean check(String s, int i, int j) {
private boolean check(int i, int j) {
for (; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
Expand All @@ -98,48 +108,26 @@ class Solution {
}
```

### **TypeScript**

```ts
function validPalindrome(s: string): boolean {
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return (
isPalinddrome(s.slice(i, j)) ||
isPalinddrome(s.slice(i + 1, j + 1))
);
}
}
return true;
}

function isPalinddrome(s: string): boolean {
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
```

### **C++**

```cpp
class Solution {
public:
bool validPalindrome(string s) {
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
if (s[i] != s[j])
return check(s, i + 1, j) || check(s, i, j - 1);
return 1;
}

bool check(string s, int i, int j) {
for (; i < j; ++i, --j)
if (s[i] != s[j])
return 0;
return 1;
auto check = [&](int i, int j) {
for (; i < j; ++i, --j) {
if (s[i] != s[j]) {
return false;
}
}
return true;
};
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
if (s[i] != s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}
};
```
Expand All @@ -165,6 +153,27 @@ func validPalindrome(s string) bool {
}
```

### **TypeScript**

```ts
function validPalindrome(s: string): boolean {
const check = (i: number, j: number): boolean => {
for (; i < j; ++i, --j) {
if (s[i] !== s[j]) {
return false;
}
}
return true;
};
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
if (s[i] !== s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}
```

### **JavaScript**

```js
Expand All @@ -173,16 +182,16 @@ func validPalindrome(s string) bool {
* @return {boolean}
*/
var validPalindrome = function (s) {
let check = function (i, j) {
const check = (i, j) => {
for (; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
if (s[i] !== s[j]) {
return false;
}
}
return true;
};
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
if (s[i] !== s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
class Solution {
public:
bool validPalindrome(string s) {
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
if (s[i] != s[j])
return check(s, i + 1, j) || check(s, i, j - 1);
return 1;
}

bool check(string s, int i, int j) {
for (; i < j; ++i, --j)
if (s[i] != s[j])
return 0;
return 1;
}
class Solution {
public:
bool validPalindrome(string s) {
auto check = [&](int i, int j) {
for (; i < j; ++i, --j) {
if (s[i] != s[j]) {
return false;
}
}
return true;
};
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
if (s[i] != s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
class Solution {
public boolean validPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return check(s, i + 1, j) || check(s, i, j - 1);
}
}
return true;
}

private boolean check(String s, int i, int j) {
for (; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
class Solution {
private String s;

public boolean validPalindrome(String s) {
this.s = s;
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
}

private boolean check(int i, int j) {
for (; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* @return {boolean}
*/
var validPalindrome = function (s) {
let check = function (i, j) {
const check = (i, j) => {
for (; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
if (s[i] !== s[j]) {
return false;
}
}
return true;
};
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
if (s[i] !== s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Solution:
def validPalindrome(self, s: str) -> bool:
def check(i, j):
while i < j:
if s[i] != s[j]:
return False
i, j = i + 1, j - 1
return True

i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return check(i, j - 1) or check(i + 1, j)
i, j = i + 1, j - 1
return True
class Solution:
def validPalindrome(self, s: str) -> bool:
def check(i: int, j: int) -> bool:
while i < j:
if s[i] != s[j]:
return False
i, j = i + 1, j - 1
return True
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return check(i + 1, j) or check(i, j - 1)
i, j = i + 1, j - 1
return True
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
function validPalindrome(s: string): boolean {
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return (
isPalinddrome(s.slice(i, j)) ||
isPalinddrome(s.slice(i + 1, j + 1))
);
const check = (i: number, j: number): boolean => {
for (; i < j; ++i, --j) {
if (s[i] !== s[j]) {
return false;
}
}
}
return true;
}

function isPalinddrome(s: string): boolean {
for (let i: number = 0, j = s.length - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
return true;
};
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
if (s[i] !== s[j]) {
return check(i + 1, j) || check(i, j - 1);
}
}
return true;
Expand Down