Skip to content

Commit 7914ef1

Browse files
committed
update: 添加问题“2109.向字符串添加空格”的代码和题解(#853)
MGJW: 周报.(本周->上周) Signed-off-by: LetMeFly666 <[email protected]>
1 parent 3ac39d6 commit 7914ef1

17 files changed

+758
-290
lines changed

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MGJW: 周报.(本周->上周)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-31 14:31:07
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-31 14:32:48
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
string addSpaces(string s, vector<int>& spaces) {
14+
string ans;
15+
for (int i = 0, j = 0; i < s.size(); i++) {
16+
if (j < spaces.size() && spaces[j] == i) {
17+
ans += ' ';
18+
j++;
19+
}
20+
ans += s[i];
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-31 14:45:51
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-31 15:18:02
6+
*/
7+
package main
8+
9+
func addSpaces(s string, spaces []int) string {
10+
ans := make([]byte, 0, len(s) + len(spaces))
11+
j := 0
12+
for i, c := range s {
13+
if j < len(spaces) && spaces[j] == i {
14+
ans = append(ans, ' ')
15+
j++
16+
}
17+
ans = append(ans, byte(c))
18+
}
19+
return string(ans)
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-31 14:37:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-31 14:44:41
6+
*/
7+
import java.lang.StringBuffer;
8+
9+
class Solution {
10+
public String addSpaces(String s, int[] spaces) {
11+
StringBuffer sb = new StringBuffer(s.length() + spaces.length);
12+
for (int i = 0, j = 0; i < s.length(); i++) {
13+
if (j < spaces.length && spaces[j] == i) {
14+
sb.append(' ');
15+
j++;
16+
}
17+
sb.append(s.charAt(i));
18+
}
19+
return sb.toString();
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-31 14:33:18
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-31 14:34:50
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def addSpaces(self, s: str, spaces: List[int]) -> str:
11+
ans = []
12+
j = 0
13+
for i, c in enumerate(s):
14+
if j < len(spaces) and spaces[j] == i:
15+
ans.append(' ')
16+
j += 1
17+
ans.append(c)
18+
return ''.join(ans)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-31 14:21:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-31 14:24:26
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
inline bool isSpace(vector<int>& spaces, int i, int j) {
14+
if (j == spaces.size()) {
15+
return false;
16+
}
17+
return i == spaces[j];
18+
}
19+
public:
20+
string addSpaces(string s, vector<int>& spaces) {
21+
string ans;
22+
for (int i = 0, j = 0; i < s.size(); i++) {
23+
if (isSpace(spaces, i, j)) {
24+
ans += ' ';
25+
j++;
26+
}
27+
ans += s[i];
28+
}
29+
return ans;
30+
}
31+
};

Codes/2109-adding-spaces-to-a-string_TestForTimeConsume.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@
22
* @Author: LetMeFly
33
* @Date: 2025-03-30 16:55:26
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-03-30 17:01:33
5+
* @LastEditTime: 2025-03-31 14:20:25
66
*/
77
#ifdef _WIN32
88
#include "_[1,2]toVector.h"
99
#endif
1010

11+
// g++ Codes\2109-adding-spaces-to-a-string_TestForTimeConsume.cpp -o temp.exe; ./temp.exe
12+
// ./temp.exe
1113
int main() {
14+
string s(100000, '0');
1215
time_t start = clock();
13-
_sleep(1000);
16+
for (int i = 0; i < 100000; i++) {
17+
s.insert(s.begin() + rand() % 10, rand() % 20);
18+
}
1419
time_t end = clock();
15-
puts("");
20+
cout << end - start << endl; // 基本上280-290ms
21+
22+
start = end;
23+
for (int i = 0; i < 100000; i++) {
24+
s[rand() % 10] = rand() % 20; // 基本上3ms
25+
}
26+
end = clock();
27+
cout << end - start << endl;
28+
29+
start = end;
30+
for (int i = 0; i < 100000; i++) {
31+
s += rand() % 20; // 基本上2ms
32+
}
33+
end = clock();
1634
cout << end - start << endl;
17-
puts("");
1835
return 0;
1936
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@
660660
|2101.引爆最多的炸弹|中等|<a href="https://leetcode.cn/problems/detonate-the-maximum-bombs/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/23/LeetCode%202101.%E5%BC%95%E7%88%86%E6%9C%80%E5%A4%9A%E7%9A%84%E7%82%B8%E5%BC%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140629296" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/detonate-the-maximum-bombs/solutions/2854053/letmefly-2101yin-bao-zui-duo-de-zha-dan-m73kr/" target="_blank">LeetCode题解</a>|
661661
|2105.给植物浇水II|中等|<a href="https://leetcode.cn/problems/watering-plants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/05/LeetCode%202105.%E7%BB%99%E6%A4%8D%E7%89%A9%E6%B5%87%E6%B0%B4II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126709258" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/watering-plants-ii/solution/by-tisfy-0erl/" target="_blank">LeetCode题解</a>|
662662
|2106.摘水果|困难|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/04/LeetCode%202106.%E6%91%98%E6%B0%B4%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130482457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/solutions/2255124/letmefly-2106zhai-shui-guo-by-tisfy-tb8v/" target="_blank">LeetCode题解</a>|
663+
|2109.向字符串添加空格|中等|<a href="https://leetcode.cn/problems/adding-spaces-to-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/31/LeetCode%202109.%E5%90%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%B7%BB%E5%8A%A0%E7%A9%BA%E6%A0%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146863199" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/adding-spaces-to-a-string/solutions/3635218/letmefly-2109xiang-zi-fu-chuan-tian-jia-cectp/" target="_blank">LeetCode题解</a>|
663664
|2116.判断一个括号字符串是否有效|中等|<a href="https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/23/LeetCode%202116.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%8B%AC%E5%8F%B7%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146454056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/solutions/3624225/letmefly-2116pan-duan-yi-ge-gua-hao-zi-f-9n2z/" target="_blank">LeetCode题解</a>|
664665
|2129.将标题首字母大写|简单|<a href="https://leetcode.cn/problems/capitalize-the-title/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/11/LeetCode%202129.%E5%B0%86%E6%A0%87%E9%A2%98%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136614914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/capitalize-the-title/solutions/2679708/letmefly-2129jiang-biao-ti-shou-zi-mu-da-nj90/" target="_blank">LeetCode题解</a>|
665666
|2132.用邮票贴满网格图|困难|<a href="https://leetcode.cn/problems/stamping-the-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/14/LeetCode%202132.%E7%94%A8%E9%82%AE%E7%A5%A8%E8%B4%B4%E6%BB%A1%E7%BD%91%E6%A0%BC%E5%9B%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135002925" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/2566644/letmefly-2132yong-you-piao-tie-man-wang-znigh/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)