Skip to content

Commit 7edf66e

Browse files
committed
feat: add solutions to lc problem: No.1592
No.1592.Rearrange Spaces Between Words
1 parent 9649470 commit 7edf66e

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

solution/1500-1599/1592.Rearrange Spaces Between Words/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,72 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:字符串模拟**
64+
65+
统计字符串 `text` 中的空格数,记为 `cnt`。将 `text` 按空格分割成字符串数组 `words`。然后计算相邻字符串之间需要拼接的空格数,进行拼接。最后将剩余的空格拼接在末尾。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 表示字符串 `text` 的长度。
68+
6369
<!-- tabs:start -->
6470

6571
### **Python3**
6672

6773
<!-- 这里可写当前语言的特殊实现逻辑 -->
6874

6975
```python
70-
76+
class Solution:
77+
def reorderSpaces(self, text: str) -> str:
78+
cnt = text.count(' ')
79+
words = text.split()
80+
m = len(words) - 1
81+
if m == 0:
82+
return words[0] + ' ' * cnt
83+
return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m)
7184
```
7285

7386
### **Java**
7487

7588
<!-- 这里可写当前语言的特殊实现逻辑 -->
7689

7790
```java
91+
class Solution {
92+
public String reorderSpaces(String text) {
93+
int cnt = 0;
94+
for (char c : text.toCharArray()) {
95+
if (c == ' ') {
96+
++cnt;
97+
}
98+
}
99+
String[] words = text.split("\\s+");
100+
List<String> res = new ArrayList<>();
101+
for (String w : words) {
102+
if (!"".equals(w)) {
103+
res.add(w);
104+
}
105+
}
106+
int m = res.size() - 1;
107+
if (m == 0) {
108+
return res.get(0) + " ".repeat(cnt);
109+
}
110+
String ans = String.join(" ".repeat(cnt / m), res);
111+
ans += " ".repeat(cnt % m);
112+
return ans;
113+
}
114+
}
115+
```
78116

117+
### **Go**
118+
119+
```go
120+
func reorderSpaces(text string) string {
121+
cnt := strings.Count(text, " ")
122+
words := strings.Fields(text)
123+
m := len(words) - 1
124+
if m == 0 {
125+
return words[0] + strings.Repeat(" ", cnt)
126+
}
127+
return strings.Join(words, strings.Repeat(" ", cnt/m)) + strings.Repeat(" ", cnt%m)
128+
}
79129
```
80130

81131
### **...**

solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,57 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def reorderSpaces(self, text: str) -> str:
48+
cnt = text.count(' ')
49+
words = text.split()
50+
m = len(words) - 1
51+
if m == 0:
52+
return words[0] + ' ' * cnt
53+
return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m)
4754
```
4855

4956
### **Java**
5057

5158
```java
59+
class Solution {
60+
public String reorderSpaces(String text) {
61+
int cnt = 0;
62+
for (char c : text.toCharArray()) {
63+
if (c == ' ') {
64+
++cnt;
65+
}
66+
}
67+
String[] words = text.split("\\s+");
68+
List<String> res = new ArrayList<>();
69+
for (String w : words) {
70+
if (!"".equals(w)) {
71+
res.add(w);
72+
}
73+
}
74+
int m = res.size() - 1;
75+
if (m == 0) {
76+
return res.get(0) + " ".repeat(cnt);
77+
}
78+
String ans = String.join(" ".repeat(cnt / m), res);
79+
ans += " ".repeat(cnt % m);
80+
return ans;
81+
}
82+
}
83+
```
5284

85+
### **Go**
86+
87+
```go
88+
func reorderSpaces(text string) string {
89+
cnt := strings.Count(text, " ")
90+
words := strings.Fields(text)
91+
m := len(words) - 1
92+
if m == 0 {
93+
return words[0] + strings.Repeat(" ", cnt)
94+
}
95+
return strings.Join(words, strings.Repeat(" ", cnt/m)) + strings.Repeat(" ", cnt%m)
96+
}
5397
```
5498

5599
### **...**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func reorderSpaces(text string) string {
2+
cnt := strings.Count(text, " ")
3+
words := strings.Fields(text)
4+
m := len(words) - 1
5+
if m == 0 {
6+
return words[0] + strings.Repeat(" ", cnt)
7+
}
8+
return strings.Join(words, strings.Repeat(" ", cnt/m)) + strings.Repeat(" ", cnt%m)
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public String reorderSpaces(String text) {
3+
int cnt = 0;
4+
for (char c : text.toCharArray()) {
5+
if (c == ' ') {
6+
++cnt;
7+
}
8+
}
9+
String[] words = text.split("\\s+");
10+
List<String> res = new ArrayList<>();
11+
for (String w : words) {
12+
if (!"".equals(w)) {
13+
res.add(w);
14+
}
15+
}
16+
int m = res.size() - 1;
17+
if (m == 0) {
18+
return res.get(0) + " ".repeat(cnt);
19+
}
20+
String ans = String.join(" ".repeat(cnt / m), res);
21+
ans += " ".repeat(cnt % m);
22+
return ans;
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def reorderSpaces(self, text: str) -> str:
3+
cnt = text.count(' ')
4+
words = text.split()
5+
m = len(words) - 1
6+
if m == 0:
7+
return words[0] + ' ' * cnt
8+
return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m)

0 commit comments

Comments
 (0)