当前位置:网站首页>LeetCode每日一题(2109. Adding Spaces to a String)
LeetCode每日一题(2109. Adding Spaces to a String)
2022-07-03 09:01: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.
假设当前要插入的 index 为 i, 当前已经在原字符串中插入了 n 个空格了, 那实际要插入的 index 就变成了 i + n
但是 rust 用这个方法完成整体耗时一秒多,可能与 String 底层的实现有关,一旦超过当前字符串所分配的容量,那一次插入就要重新分配一次内存, 不过不知道这种重分配会多频繁, 应该不至于每次插入都重新分配吧。改天用 split 然后再拼接的方法做一次,看看速度会不会快一点
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
}
}
边栏推荐
- The "booster" of traditional office mode, Building OA office system, was so simple!
- [point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
- Word segmentation in full-text indexing
- Hudi 快速体验使用(含操作详细步骤及截图)
- Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
- ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
- Overview of image restoration methods -- paper notes
- LeetCode 871. Minimum refueling times
- Beego learning - Tencent cloud upload pictures
- LeetCode 515. Find the maximum value in each tree row
猜你喜欢
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
LeetCode 57. Insert interval
Utilisation de hudi dans idea
IDEA 中使用 Hudi
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Spark 结构化流写入Hudi 实践
【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
LeetCode 241. Design priorities for operational expressions
随机推荐
Basic knowledge of database design
AcWing 785. Quick sort (template)
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
Utilisation de hudi dans idea
LeetCode 515. Find the maximum value in each tree row
CSDN markdown editor help document
The "booster" of traditional office mode, Building OA office system, was so simple!
Hudi integrated spark data analysis example (including code flow and test results)
Crawler career from scratch (IV): climb the bullet curtain of station B through API
2022-2-14 learning the imitation Niuke project - send email
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
307. Range Sum Query - Mutable
Problems in the implementation of lenet
Flink学习笔记(八)多流转换
Construction of simple database learning environment
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
IDEA 中使用 Hudi