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

Commit 4a70932

Browse files
✨feat: add 1592
1 parent ad463e6 commit 4a70932

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
| [1446. 连续字符](https://leetcode-cn.com/problems/consecutive-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/consecutive-characters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xtv6/) | 简单 | 🤩🤩🤩🤩🤩 |
5454
| [1455. 检查单词是否为句中其他单词的前缀](https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/solution/by-ac_oier-cfsi/) | 简单 | 🤩🤩🤩🤩🤩 |
5555
| [1537. 最大得分](https://leetcode.cn/problems/get-the-maximum-score/) | [LeetCode 题解链接](https://leetcode.cn/problems/get-the-maximum-score/solution/by-ac_oier-ht78/) | 困难 | 🤩🤩🤩🤩 |
56+
| [1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/) | [LeetCode 题解链接](https://leetcode.cn/problems/rearrange-spaces-between-words/solution/by-ac_oier-0f5h/) | 简单 | 🤩🤩🤩 |
5657
| [1610. 可见点的最大数目](https://leetcode-cn.com/problems/maximum-number-of-visible-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-visible-points/solution/gong-shui-san-xie-qiu-ji-jiao-ji-he-ti-b-0bid/) | 困难 | 🤩🤩🤩🤩 |
5758
| [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 |
5859
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
| [1576. 替换所有的问号](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-fa1u/) | 简单 | 🤩🤩🤩🤩🤩 |
163163
| [1582. 二进制矩阵中的特殊位置](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/solution/by-ac_oier-61w0/) | 简单 | 🤩🤩🤩🤩🤩 |
164164
| [1583. 统计不开心的朋友](https://leetcode-cn.com/problems/count-unhappy-friends/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-unhappy-friends/solution/gong-shui-san-xie-ha-xi-biao-mo-ni-ti-by-2qy0/) | 中等 | 🤩🤩🤩🤩 |
165+
| [1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/) | [LeetCode 题解链接](https://leetcode.cn/problems/rearrange-spaces-between-words/solution/by-ac_oier-0f5h/) | 简单 | 🤩🤩🤩🤩 |
165166
| [1606. 找到处理最多请求的服务器](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/solution/by-ac_oier-zgm6/) | 困难 | 🤩🤩🤩🤩 |
166167
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
167168
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/solution/by-ac_oier-0f5h/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「双指针」
6+
7+
8+
9+
给你一个字符串 `text`,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 `text` 至少包含一个单词 。
10+
11+
请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 `text` 字符串的长度相等。
12+
13+
返回 重新排列空格后的字符串 。
14+
15+
示例 1:
16+
```
17+
输入:text = " this is a sentence "
18+
19+
输出:"this is a sentence"
20+
21+
解释:总共有 9 个空格和 4 个单词。可以将 9 个空格平均分配到相邻单词之间,相邻单词间空格数为:9 / (4-1) = 3 个。
22+
```
23+
示例 2:
24+
```
25+
输入:text = " practice makes perfect"
26+
27+
输出:"practice makes perfect "
28+
29+
解释:总共有 7 个空格和 3 个单词。7 / (3-1) = 3 个空格加上 1 个多余的空格。多余的空格需要放在字符串的末尾。
30+
```
31+
示例 3:
32+
```
33+
输入:text = "hello world"
34+
35+
输出:"hello world"
36+
```
37+
示例 4:
38+
```
39+
输入:text = " walks udp package into bar a"
40+
41+
输出:"walks udp package into bar a "
42+
```
43+
示例 5:
44+
```
45+
输入:text = "a"
46+
47+
输出:"a"
48+
```
49+
50+
提示:
51+
* 1ドル <= text.length <= 100$
52+
* `text` 由小写英文字母和 `' '` 组成
53+
* `text` 中至少包含一个单词
54+
55+
---
56+
57+
### 模拟
58+
59+
根据题意模拟即可:使用「双指针」统计空格数量和分割出所有单词。
60+
61+
Java 代码:
62+
```Java
63+
class Solution {
64+
public String reorderSpaces(String s) {
65+
int n = s.length(), cnt = 0;
66+
List<String> list = new ArrayList<>();
67+
for (int i = 0; i < n; ) {
68+
if (s.charAt(i) == ' ' && ++i >= 0 && ++cnt >= 0) continue;
69+
int j = i;
70+
while (j < n && s.charAt(j) != ' ') j++;
71+
list.add(s.substring(i, j));
72+
i = j;
73+
}
74+
StringBuilder sb = new StringBuilder();
75+
int m = list.size(), t = cnt / Math.max(m - 1, 1);
76+
String k = "";
77+
while (t-- > 0) k += " ";
78+
for (int i = 0; i < m; i++) {
79+
sb.append(list.get(i));
80+
if (i != m - 1) sb.append(k);
81+
}
82+
while (sb.length() != n) sb.append(" ");
83+
return sb.toString();
84+
}
85+
}
86+
```
87+
TypeScript 代码:
88+
```TypeScript
89+
function reorderSpaces(s: string): string {
90+
let n = s.length, cnt = 0
91+
const list = new Array<string>()
92+
for (let i = 0; i < n; ) {
93+
if (s[i] == ' ' && ++i >= 0 && ++cnt >= 0) continue
94+
let j = i + 1
95+
while (j < n && s[j] != ' ') j++
96+
list.push(s.substring(i, j))
97+
i = j
98+
}
99+
let ans = '', k = ''
100+
let m = list.length, t = Math.floor(cnt / Math.max(m - 1, 1))
101+
while (t-- > 0) k += ' '
102+
for (let i = 0; i < m; i++) {
103+
ans += list[i]
104+
if (i != m - 1) ans += k
105+
}
106+
while (ans.length != n) ans += ' '
107+
return ans
108+
};
109+
```
110+
* 时间复杂度:$O(n)$
111+
* 空间复杂度:$O(n)$
112+
113+
---
114+
115+
### 最后
116+
117+
这是我们「刷穿 LeetCode」系列文章的第 `No.1592` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
118+
119+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
120+
121+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
122+
123+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
124+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /