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

Commitdbfcdba

Browse files
refactor 917
1 parentf5c89a0 commitdbfcdba

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

‎README.md

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

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java)| O(n)| O(n)||Easy|
3233
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java)| O(n)| O(1)||Easy|
3334
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java)| O(n)| O(1)||Easy|
3435
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java)| O(n)| O(k)||Easy|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 917. Reverse Only Letters
5+
*
6+
* Given a string S, return the "reversed" string where all characters
7+
* that are not a letter stay in the same place, and all letters reverse their positions.
8+
*
9+
* Example 1:
10+
*
11+
* Input: "ab-cd"
12+
* Output: "dc-ba"
13+
* Example 2:
14+
*
15+
* Input: "a-bC-dEf-ghIj"
16+
* Output: "j-Ih-gfE-dCba"
17+
* Example 3:
18+
*
19+
* Input: "Test1ng-Leet=code-Q!"
20+
* Output: "Qedo1ct-eeLg=ntse-T!"
21+
*
22+
*
23+
* Note:
24+
*
25+
* S.length <= 100
26+
* 33 <= S[i].ASCIIcode <= 122
27+
* S doesn't contain \ or "
28+
* */
29+
publicclass_917 {
30+
publicstaticclassSolution1 {
31+
publicStringreverseOnlyLetters(StringS) {
32+
char[]array =S.toCharArray();
33+
for (inti =0,j =array.length -1;i <j;) {
34+
if (Character.isLetter(array[i]) &&Character.isLetter(array[j])) {
35+
chartemp =array[i];
36+
array[i++] =array[j];
37+
array[j--] =temp;
38+
}elseif (Character.isLetter(array[i])) {
39+
j--;
40+
}elseif (Character.isLetter(array[j])){
41+
i++;
42+
}else {
43+
i++;
44+
j--;
45+
}
46+
}
47+
returnnewString(array);
48+
}
49+
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._917;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_917Test {
10+
privatestatic_917.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_917.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("dc-ba",solution1.reverseOnlyLetters("ab-cd"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals("j-Ih-gfE-dCba",solution1.reverseOnlyLetters("a-bC-dEf-ghIj"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals("Qedo1ct-eeLg=ntse-T!",solution1.reverseOnlyLetters("Test1ng-Leet=code-Q!"));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp