目录
题目链接:2109. 向字符串添加空格 - 力扣(LeetCode)
题目链接:2109. 向字符串添加空格 - 力扣(LeetCode)
注:下述题目描述和示例均来自力扣
题目描述
给你一个下标从 0 开始的字符串 s
,以及一个下标从 0 开始的整数数组 spaces
。
数组 spaces
描述原字符串中需要添加空格的下标。每个空格都应该插入到给定索引处的字符值 之前 。
- 例如,
s = "EnjoyYourCoffee"
且spaces = [5, 9]
,那么我们需要在'Y'
和'C'
之前添加空格,这两个字符分别位于下标5
和下标9
。因此,最终得到"Enjoy Your Coffee"
。
请你添加空格,并返回修改后的字符串。
示例 1:
输入:s = "LeetcodeHelpsMeLearn", spaces = [8,13,15] 输出:"Leetcode Helps Me Learn" 解释: 下标 8、13 和 15 对应 "LeetcodeHelpsMeLearn" 中加粗斜体字符。 接着在这些字符前添加空格。
示例 2:
输入:s = "icodeinpython", spaces = [1,5,7,9] 输出:"i code in py thon" 解释: 下标 1、5、7 和 9 对应 "icodeinpython" 中加粗斜体字符。 接着在这些字符前添加空格。
示例 3:
输入:s = "spacing", spaces = [0,1,2,3,4,5,6] 输出:" s p a c i n g" 解释: 字符串的第一个字符前可以添加空格。
提示:
1 <= s.length <= 3 *
s
仅由大小写英文字母组成1 <= spaces.length <= 3 *
0 <= spaces[i] <= s.length - 1
spaces
中的所有值 严格递增
解法一:拼接字符串
通过一次遍历字符串 s
,在遇到 spaces
数组中指定的位置时,插入空格。具体步骤如下:
1. 初始化变量
- 定义一个索引变量
index
,用于跟踪当前需要处理的spaces
数组中的位置。 - 定义一个结果字符串
result
,用于存储最终生成的字符串。 - 获取
spaces
数组的长度len
,以便判断是否已经处理完所有需要插入空格的位置。
2. 遍历字符串
- 使用一个循环从左到右遍历字符串
s
的每个字符。 - 对于每个字符的位置
i
:- 检查是否满足条件:
index < len && spaces[index] == i
。- 如果满足,则说明当前位置
i
是需要插入空格的位置。 - 在结果字符串
result
中先添加一个空格,然后将index
自增,指向下一个需要插入空格的位置。
- 如果满足,则说明当前位置
- 无论是否插入空格,都将当前字符
s[i]
添加到结果字符串result
中。
- 检查是否满足条件:
3. 返回结果
- 当遍历完成后,结果字符串
result
就是插入空格后的最终字符串。
代码逻辑细节
-
为什么用
index
跟踪spaces
数组?- 因为
spaces
数组中的元素是按升序排列的(题目隐含前提),所以我们可以用一个指针index
来逐步匹配需要插入空格的位置。 - 这样可以避免每次都从头开始查找
spaces
数组,提高效率。
- 因为
-
如何判断是否需要插入空格?
- 判断条件是:
index < len && spaces[index] == i
。index < len
确保不会越界访问spaces
数组。spaces[index] == i
判断当前下标i
是否是需要插入空格的位置。
- 判断条件是:
-
为什么用
+=
拼接字符串?- 在 C++ 中,
std::string
支持直接使用+=
操作符进行拼接操作,简单高效。 - 当然了,在java中直接采用StringBuffer就可以了。
- 在 C++ 中,
过程演示
以输入 s = "LeetcodeHelpsMeLearn"
和 spaces = [8, 13, 15]
为例,代码的执行过程如下:
-
初始化:
index = 0
result = ""
-
遍历字符串
s
:i = 0
,字符为'L'
,不满足spaces[index] == i
,result = "L"
。i = 1
,字符为'e'
,不满足,result = "Le"
。- ...
i = 8
,字符为'H'
,满足spaces[index] == i
,插入空格,result = "Leetcode "
,index = 1
。i = 9
,字符为'e'
,不满足,result = "Leetcode H"
。- ...
i = 13
,字符为'M'
,满足spaces[index] == i
,插入空格,result = "Leetcode Helps "
,index = 2
。- ...
i = 15
,字符为'L'
,满足spaces[index] == i
,插入空格,result = "Leetcode Helps Me "
,index = 3
。- ...
- 最终,
result = "Leetcode Helps Me Learn"
。
-
返回结果:
"Leetcode Helps Me Learn"
。
Java写法:
class Solution {
public String addSpaces(String s, int[] spaces) {
// 我们直接遍历字符串在遇到对应下标的字符串的时候直接添加空格即可
int index = 0;
int len = spaces.length;
StringBuffer sb = new StringBuffer();
for(int i = 0;i < s.length();i++){
if(index < len && spaces[index] == i){
// 那么就先添加空格
sb.append(" ");
index++;
}
sb.append(s.charAt(i));
}
return sb.toString();
}
}
C++写法:
#include <string>
using namespace std;
class Solution {
public:
string addSpaces(string s, vector<int>& spaces) {
// 我们直接遍历字符串,在遇到对应下标的字符时直接添加空格即可
int index = 0; // 用于跟踪 spaces 数组的当前下标
int len = spaces.size(); // spaces 数组的长度
string result; // 用于存储结果字符串
for (int i = 0; i < s.length(); i++) {
if (index < len && spaces[index] == i) {
// 如果当前下标 i 等于 spaces[index],则先添加一个空格
result += " ";
index++; // 移动到下一个需要插入空格的位置
}
result += s[i]; // 添加当前字符
}
return result; // 返回最终结果
}
};
运行时间
时间复杂度和空间复杂度
- 时间复杂度:O(n),其中
n
是字符串s
的长度。我们只需遍历一次字符串即可完成操作。 - 空间复杂度:O(n),因为我们需要额外的空间来存储结果字符串
result
。
解法二:方法一的极致优化
- 预分配
StringBuilder
的容量:减少动态扩容的开销。 - 使用单个字符追加:避免不必要的字符串操作。
- 提前终止循环:减少多余的条件检查。
Java写法:
class Solution {
public String addSpaces(String s, int[] spaces) {
int index = 0; // 用于跟踪 spaces 数组的当前下标
int len = spaces.length; // spaces 数组的长度
// 预分配 StringBuilder 的容量
StringBuilder sb = new StringBuilder(s.length() + spaces.length);
for (int i = 0; i < s.length(); i++) {
if (index < len && spaces[index] == i) {
sb.append(' ');
index++;
}
sb.append(s.charAt(i));
// 如果所有空格都已插入,则直接拼接剩余部分
if (index == len) {
sb.append(s.substring(i + 1)); // 拼接剩余部分
break;
}
}
return sb.toString(); // 返回最终结果
}
}
运行时间
遥遥领先有没有!!!
时间复杂度和空间复杂度
不变
总结
j经过主播的极限操作,这段代码已经尽可能地优化了性能。奥耶~