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

Commitb740f89

Browse files
add 1592
1 parent53e47de commitb740f89

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-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+
|1592|[Rearrange Spaces Between Words](https://leetcode.com/problems/rearrange-spaces-between-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1592.java)||String|Easy|
1112
|1583|[Count Unhappy Friends](https://leetcode.com/problems/count-unhappy-friends/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1583.java)||Array|Medium|
1213
|1582|[Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1582.java)||Array|Easy|
1314
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1576.java)|[:tv:](https://youtu.be/SJBDLYqrIsM)|String|Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1592 {
4+
publicstaticclassSolution1 {
5+
publicStringreorderSpaces(Stringtext) {
6+
intspaceCount =0;
7+
for (charc :text.toCharArray()) {
8+
if (c ==' ') {
9+
spaceCount++;
10+
}
11+
}
12+
String[]words =text.trim().split("\\s+");
13+
if (words.length ==1) {
14+
StringBuildersb =newStringBuilder(words[0]);
15+
for (inti =0;i <spaceCount;i++) {
16+
sb.append(" ");
17+
}
18+
returnsb.toString();
19+
}
20+
inttrailingSpaces =spaceCount % (words.length -1);
21+
intnewSpaces =spaceCount / (words.length -1);
22+
StringBuildersb =newStringBuilder();
23+
for (intj =0;j <words.length;j++) {
24+
sb.append(words[j]);
25+
if (j <words.length -1) {
26+
for (inti =0;i <newSpaces;i++) {
27+
sb.append(" ");
28+
}
29+
}else {
30+
for (inti =0;i <trailingSpaces;i++) {
31+
sb.append(" ");
32+
}
33+
}
34+
}
35+
returnsb.toString();
36+
}
37+
}
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1592;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1592Test {
10+
privatestatic_1592.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1592.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("hello ",solution1.reorderSpaces(" hello"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals("this is a sentence",solution1.reorderSpaces(" this is a sentence "));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals("practice makes perfect ",solution1.reorderSpaces(" practice makes perfect"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals("hello world",solution1.reorderSpaces("hello world"));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals("walks udp package into bar a ",solution1.reorderSpaces(" walks udp package into bar a"));
40+
}
41+
42+
@Test
43+
publicvoidtest6() {
44+
assertEquals("a",solution1.reorderSpaces("a"));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp