当前位置:网站首页>Leetcode daily question (2109. adding spaces to a string)
Leetcode daily question (2109. adding spaces to a string)
2022-07-03 09:33:00 【wangjun861205】
You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.
For example, given s = “EnjoyYourCoffee” and spaces = [5, 9], we place spaces before ‘Y’ and ‘C’, which are at indices 5 and 9 respectively. Thus, we obtain “Enjoy Your Coffee”.
Return the modified string after the spaces have been added.
Example 1:
Input: s = “LeetcodeHelpsMeLearn”, spaces = [8,13,15]
Output: “Leetcode Helps Me Learn”
Explanation:
The indices 8, 13, and 15 correspond to the underlined characters in “LeetcodeHelpsMeLearn”.
We then place spaces before those characters.
Example 2:
Input: s = “icodeinpython”, spaces = [1,5,7,9]
Output: “i code in py thon”
Explanation:
The indices 1, 5, 7, and 9 correspond to the underlined characters in “icodeinpython”.
We then place spaces before those characters.
Example 3:
Input: s = “spacing”, spaces = [0,1,2,3,4,5,6]
Output: " s p a c i n g"
Explanation:
We are also able to place spaces before the first character of the string.
Constraints:
- 1 <= s.length <= 3 * 105
- s consists only of lowercase and uppercase English letters.
- 1 <= spaces.length <= 3 * 105
- 0 <= spaces[i] <= s.length - 1
- All the values of spaces are strictly increasing.
Suppose you are currently inserting index by i, Currently, we have inserted n There are two spaces , What actually needs to be inserted index It becomes i + n
however rust It takes more than a second to complete the whole with this method , Possible and String The underlying implementation is related to , Once the allocated capacity of the current string is exceeded , That insertion requires a reallocation of memory , But I don't know how often this reallocation will happen , It should not be reallocated every time you insert . Use it another day split Then do it again by splicing , See if the speed will be faster
impl Solution {
pub fn add_spaces(mut s: String, spaces: Vec<i32>) -> String {
let mut added = 0;
for si in spaces {
s.insert(si as usize + added, ' ');
added += 1;
}
s
}
}
边栏推荐
- Idea uses the MVN command to package and report an error, which is not available
- Patent inquiry website
- What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
- Spark overview
- [point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
- Numerical analysis notes (I): equation root
- Win10安装ELK
- PolyWorks script development learning notes (II) -treeview basic operations
- 1922. Count Good Numbers
- Jestson Nano 从tftp服务器下载更新kernel和dtb
猜你喜欢
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Please tell me how to set vscode
Solve editor MD uploads pictures and cannot get the picture address
图像修复方法研究综述----论文笔记
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
Hudi learning notes (III) analysis of core concepts
About the configuration of vs2008+rade CATIA v5r22
Win10 quick screenshot
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
随机推荐
解决Editor.md上传图片获取不到图片地址问题
[CSDN]C1训练题解析_第三部分_JS基础
Logstash+jdbc data synchronization +head display problems
Windows安装Redis详细步骤
PolyWorks script development learning notes (II) -treeview basic operations
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
Idea uses the MVN command to package and report an error, which is not available
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
从0开始使用pnpm构建一个Monorepo方式管理的demo
1922. Count Good Numbers
Leetcode daily question (516. long palindromic subsequence)
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Go language - Reflection
Utilisation de hudi dans idea
用Redis实现分布式锁
Numerical analysis notes (I): equation root
Redis learning (I)
Apply for domain name binding IP to open port 80 record
Leetcode daily question (1362. closest divisors)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion