diff --git "a/Index/\345\217\214\346\214\207\351\222\210.md" "b/Index/\345\217\214\346\214\207\351\222\210.md" index cb7fffbd..1ae497e8 100644 --- "a/Index/\345\217\214\346\214\207\351\222\210.md" +++ "b/Index/\345\217\214\346\214\207\351\222\210.md" @@ -53,6 +53,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [1537. 最大得分](https://leetcode.cn/problems/get-the-maximum-score/) | [LeetCode 题解链接](https://leetcode.cn/problems/get-the-maximum-score/solution/by-ac_oier-ht78/) | 困难 | 🤩🤩🤩🤩 | +| [1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/) | [LeetCode 题解链接](https://leetcode.cn/problems/rearrange-spaces-between-words/solution/by-ac_oier-0f5h/) | 简单 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 7ec2b581..e25ec4fc 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -162,6 +162,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/) | [LeetCode 题解链接](https://leetcode.cn/problems/rearrange-spaces-between-words/solution/by-ac_oier-0f5h/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/1591-1600/1592. \351\207\215\346\226\260\346\216\222\345\210\227\345\215\225\350\257\215\351\227\264\347\232\204\347\251\272\346\240\274\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1591-1600/1592. \351\207\215\346\226\260\346\216\222\345\210\227\345\215\225\350\257\215\351\227\264\347\232\204\347\251\272\346\240\274\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..62564f5b --- /dev/null +++ "b/LeetCode/1591-1600/1592. \351\207\215\346\226\260\346\216\222\345\210\227\345\215\225\350\257\215\351\227\264\347\232\204\347\251\272\346\240\274\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,124 @@ +### 题目描述 + +这是 LeetCode 上的 **[1592. 重新排列单词间的空格](https://leetcode.cn/problems/rearrange-spaces-between-words/solution/by-ac_oier-0f5h/)** ,难度为 **简单**。 + +Tag : 「模拟」、「双指针」 + + + +给你一个字符串 `text`,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 `text` 至少包含一个单词 。 + +请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 `text` 字符串的长度相等。 + +返回 重新排列空格后的字符串 。 + +示例 1: +``` +输入:text = " this is a sentence " + +输出:"this is a sentence" + +解释:总共有 9 个空格和 4 个单词。可以将 9 个空格平均分配到相邻单词之间,相邻单词间空格数为:9 / (4-1) = 3 个。 +``` +示例 2: +``` +输入:text = " practice makes perfect" + +输出:"practice makes perfect " + +解释:总共有 7 个空格和 3 个单词。7 / (3-1) = 3 个空格加上 1 个多余的空格。多余的空格需要放在字符串的末尾。 +``` +示例 3: +``` +输入:text = "hello world" + +输出:"hello world" +``` +示例 4: +``` +输入:text = " walks udp package into bar a" + +输出:"walks udp package into bar a " +``` +示例 5: +``` +输入:text = "a" + +输出:"a" +``` + +提示: +* $1 <= text.length <= 100$ +* `text` 由小写英文字母和 `' '` 组成 +* `text` 中至少包含一个单词 + +--- + +### 模拟 + +根据题意模拟即可:使用「双指针」统计空格数量和分割出所有单词。 + +Java 代码: +```Java +class Solution { + public String reorderSpaces(String s) { + int n = s.length(), cnt = 0; + List list = new ArrayList<>(); + for (int i = 0; i < n; ) { + if (s.charAt(i) == ' ' && ++i >= 0 && ++cnt >= 0) continue; + int j = i; + while (j < n && s.charAt(j) != ' ') j++; + list.add(s.substring(i, j)); + i = j; + } + StringBuilder sb = new StringBuilder(); + int m = list.size(), t = cnt / Math.max(m - 1, 1); + String k = ""; + while (t-- > 0) k += " "; + for (int i = 0; i < m; i++) { + sb.append(list.get(i)); + if (i != m - 1) sb.append(k); + } + while (sb.length() != n) sb.append(" "); + return sb.toString(); + } +} +``` +TypeScript 代码: +```TypeScript +function reorderSpaces(s: string): string { + let n = s.length, cnt = 0 + const list = new Array() + for (let i = 0; i < n; ) { + if (s[i] == ' ' && ++i >= 0 && ++cnt >= 0) continue + let j = i + 1 + while (j < n && s[j] != ' ') j++ + list.push(s.substring(i, j)) + i = j + } + let ans = '', k = '' + let m = list.length, t = Math.floor(cnt / Math.max(m - 1, 1)) + while (t-- > 0) k += ' ' + for (let i = 0; i < m; i++) { + ans += list[i] + if (i != m - 1) ans += k + } + while (ans.length != n) ans += ' ' + return ans +}; +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1592` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +