Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf8f045f

Browse files
add 680
1 parent5a1441f commitf8f045f

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|680|[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_680.java) | O(n) | O(1) | Easy | String
2526
|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java)| O(n^2)| O(n)| Medium|
2627
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java)| O(n^2)| O(1)| Easy|
2728
|672|[Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_672.java) | O(1) | O(1) | Medium | Math
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 680. Valid Palindrome II
5+
*
6+
* Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
7+
8+
Example 1:
9+
Input: "aba"
10+
Output: True
11+
12+
Example 2:
13+
Input: "abca"
14+
Output: True
15+
Explanation: You could delete the character 'c'.
16+
17+
Note:
18+
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
19+
20+
*/
21+
publicclass_680 {
22+
publicstaticclassSolution1 {
23+
publicbooleanvalidPalindrome(Strings) {
24+
intleft =0;
25+
intright =s.length() -1;
26+
intdiff =0;
27+
while (left <right) {
28+
if (s.charAt(left) !=s.charAt(right)) {
29+
left++;
30+
diff++;
31+
if (diff >1) {
32+
break;
33+
}
34+
}else {
35+
left++;
36+
right--;
37+
}
38+
}
39+
if (diff <2) {
40+
returntrue;
41+
}
42+
diff =0;
43+
left =0;
44+
right =s.length() -1;
45+
while (left <right) {
46+
if (s.charAt(left) !=s.charAt(right)) {
47+
right--;
48+
diff++;
49+
if (diff >1) {
50+
break;
51+
}
52+
}else {
53+
left++;
54+
right--;
55+
}
56+
}
57+
returndiff <2;
58+
}
59+
}
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._680;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_680Test {
10+
privatestatic_680.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_680.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.validPalindrome("aba"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(true,solution1.validPalindrome("abcca"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(true,solution1.validPalindrome("acbca"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(false,solution1.validPalindrome("accbba"));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals(true,solution1.validPalindrome("abdeeda"));
40+
}
41+
42+
@Test
43+
publicvoidtest6() {
44+
assertEquals(true,solution1.validPalindrome("cbbcc"));
45+
}
46+
47+
@Test
48+
publicvoidtest7() {
49+
assertEquals(false,solution1.validPalindrome("abc"));
50+
}
51+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp