当前位置:网站首页>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
}
}
边栏推荐
- Esp32 at command does not respond
- LeetCode每日一题(2090. K Radius Subarray Averages)
- Learning C language from scratch -- installation and configuration of 01 MinGW
- 文件系统中的目录与切换操作
- Win10 quick screenshot
- IDEA 中使用 Hudi
- Long类型的相等判断
- Serializer rewrite: update and create methods
- unbuntu(debian)下TFTP服务器搭建及测试
- IDEA 中使用 Hudi
猜你喜欢
Solve the problem of disordered code in vscode development, output Chinese and open source code
Arduino handles JSON data, arduinojson assistant
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
Common software open source protocols
There is no open in default browser option in the right click of the vscade editor
Construction of simple database learning environment
随机推荐
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Installation and uninstallation of pyenv
Alibaba cloud notes for the first time
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Trial of the combination of RDS and crawler
Quickly use markdown to edit articles
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
Go language - JSON processing
Spark 概述
Hudi quick experience (including detailed operation steps and screenshots)
Equality judgment of long type
Numerical analysis notes (I): equation root
Jestson Nano 从tftp服务器下载更新kernel和dtb
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
LeetCode每日一题(2109. Adding Spaces to a String)
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)
Apply for domain name binding IP to open port 80 record
unbuntu(debian)下TFTP服务器搭建及测试
Spark overview
Jetson nano custom boot icon kernel logo CBOOT logo