Skip to content

Commit 4452e1d

Browse files
committed
feat: add the solution of Valid Palindrome II(680) with Java.
1 parent dea2774 commit 4452e1d

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
| [669][669-question] | [Trim a Binary Search Tree][669-tips] | [][669-java] | [][669-js] | |
7272
| [671][671-question] | [Second Minimum Node In a Binary Tree][671-tips] | [][671-java] | [][671-js] | |
7373
| [674][674-question] | [Longest Continuous Increasing Subsequence][674-tips] | [][674-java] | [][674-js] | |
74-
| [680][680-question] | [Valid Palindrome II][680-tips] | | [][680-js] | |
74+
| [680][680-question] | [Valid Palindrome II][680-tips] | [][680-java] | [][680-js] | |
7575
| [682][682-question] | [Baseball Game][682-tips] | | [][682-js] | |
7676
| [686][686-question] | [Repeated String Match][686-tips] | | [][686-js] | |
7777
| [687][687-question] | [Longest Univalue Path][687-tips] | | [][687-js] | |
@@ -464,6 +464,7 @@
464464
[669-java]: ./src/_669/Solution.java
465465
[671-java]: ./src/_671/Solution.java
466466
[674-java]: ./src/_674/Solution.java
467+
[680-java]: ./src/_680/Solution.java
467468
[728-java]: ./src/_728/Solution.java
468469
[771-java]: ./src/_771/Solution.java
469470
[804-java]: ./src/_804/Solution.java

src/_680/Solution.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package _680;
2+
3+
class Solution {
4+
public boolean validPalindrome(String s) {
5+
int l = 0;
6+
int r = s.length() - 1;
7+
while (l < r) {
8+
if (s.charAt(l) != s.charAt(r)) {
9+
return isPalindrome(s, l + 1, r) || isPalindrome(s, l, r - 1);
10+
}
11+
l++;
12+
r--;
13+
}
14+
return l >= r;
15+
}
16+
17+
private boolean isPalindrome(String s, int l, int r) {
18+
while (l < r) {
19+
if (s.charAt(l) != s.charAt(r)) {
20+
return false;
21+
}
22+
l++;
23+
r--;
24+
}
25+
return true;
26+
}
27+
28+
public static void main(String[] args) {
29+
Solution solution = new Solution();
30+
System.out.println(solution.validPalindrome("aba"));
31+
System.out.println(solution.validPalindrome("abca"));
32+
System.out.println(solution.validPalindrome("ececcec"));
33+
}
34+
}

tips/680/README.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
1-
[xxxx][title]
1+
[Valid Palindrome II][title]
22

33
## Description
4-
// 抄题目
4+
Given a non-empty string `s`, you may delete **at most** one character. Judge whether you can make it a palindrome.
55

6+
**Example 1:**
67

7-
**Example:**
8+
```
9+
Input: "aba"
10+
Output: True
11+
```
12+
13+
**Example 2:**
814

915
```
10-
// 抄Example
16+
Input: "abca"
17+
Output: True
18+
Explanation: You could delete the character 'c'.
1119
```
1220

1321
**Note:**
14-
// Note
1522

16-
**Tags:** // tags
23+
1. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
1724

25+
**Tags:** [String](https://leetcode.com/tag/string/)
1826

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
27+
## 思路
2328

24-
```
25-
```javascript
29+
给出一个字符串,要求判断是否可以通过删除最多一个字符来使得原字符串变成回文字符串。对原字符串进行回文判断,若发现左右不匹配的情况下,尝试删除左边或右边的字符,再进行判断,只要其中一种删除方式能使原字符串成为回文字符串则判断可行,若两种删除方式均不行则判断为不可行。
2630

27-
```
31+
**Java:**
2832

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
3133
```java
32-
33-
```
34-
35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
38-
34+
class Solution {
35+
public boolean validPalindrome(String s) {
36+
int l = 0;
37+
int r = s.length() - 1;
38+
while (l < r) {
39+
if (s.charAt(l) != s.charAt(r)) {
40+
return isPalindrome(s, l + 1, r) || isPalindrome(s, l, r - 1);
41+
}
42+
l++;
43+
r--;
44+
}
45+
return l >= r;
46+
}
47+
48+
private boolean isPalindrome(String s, int l, int r) {
49+
while (l < r) {
50+
if (s.charAt(l) != s.charAt(r)) {
51+
return false;
52+
}
53+
l++;
54+
r--;
55+
}
56+
return true;
57+
}
58+
}
3959
```
4060

4161
## 结语
42-
62+
4363
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4464

45-
[title]: https://leetcode.com/problems/xxxx
65+
[title]: https://leetcode.com/problems/valid-palindrome-ii/description/
4666
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)