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

Commitfd32110

Browse files
add 1694
1 parent9e13784 commitfd32110

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1694|[Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1694.java)||Easy|String|
1112
|1690|[Stone Game VII](https://leetcode.com/problems/stone-game-vii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1690.java)||Medium|DP|
1213
|1688|[Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1688.java)||Easy|Backtracking|
1314
|1685|[Sum of Absolute Differences in a Sorted Array](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1685.java)||Medium|Math, Greedy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1694 {
4+
publicstaticclassSolution1 {
5+
publicStringreformatNumber(Stringnumber) {
6+
StringBuildersb =newStringBuilder();
7+
for (charc :number.toCharArray()) {
8+
if (Character.isDigit(c)) {
9+
sb.append(c);
10+
}
11+
}
12+
Stringcleaned =sb.toString();
13+
sb.setLength(0);
14+
for (inti =0;i <cleaned.length(); ) {
15+
if (i +4 ==cleaned.length()) {
16+
sb.append(cleaned.substring(i,i +2));
17+
sb.append("-");
18+
sb.append(cleaned.substring(i +2));
19+
break;
20+
}elseif (i +3 <=cleaned.length()) {
21+
sb.append(cleaned.substring(i,i +3));
22+
sb.append("-");
23+
i +=3;
24+
}else {
25+
sb.append(cleaned.substring(i));
26+
break;
27+
}
28+
}
29+
if (sb.charAt(sb.length() -1) =='-') {
30+
sb.setLength(sb.length() -1);
31+
}
32+
returnsb.toString();
33+
}
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1694;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1694Test {
10+
privatestatic_1694.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1694.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("123-456",solution1.reformatNumber("1-23-45 6"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals("123-45-67",solution1.reformatNumber("123 4-567"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals("123-456-78",solution1.reformatNumber("123 4-5678"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals("12",solution1.reformatNumber("12"));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals("175-229-353-94-75",solution1.reformatNumber("--17-5 229 35-39475 "));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp