当前位置:网站首页>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
}
}
边栏推荐
- Crawler career from scratch (V): detailed explanation of re regular expression
- Liteide is easy to use
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- Derivation of Fourier transform
- Spark 结构化流写入Hudi 实践
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
- ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
- Integrated use of interlij idea and sonarqube
- Solve editor MD uploads pictures and cannot get the picture address
猜你喜欢

Vscode Arduino installation Library

Hudi 快速体验使用(含操作详细步骤及截图)

【Kotlin学习】类、对象和接口——定义类继承结构

Go language - Reflection

Analysis of the implementation principle of an open source markdown to rich text editor

Solve editor MD uploads pictures and cannot get the picture address
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

Spark 概述

Directory and switching operation in file system

IDEA 中使用 Hudi
随机推荐
Liteide is easy to use
Spark structured stream writing Hudi practice
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
LeetCode每日一题(985. Sum of Even Numbers After Queries)
1922. Count Good Numbers
Learning C language from scratch -- installation and configuration of 01 MinGW
Jetson nano custom boot icon kernel logo CBOOT logo
Analysis of the implementation principle of an open source markdown to rich text editor
【Kotlin学习】类、对象和接口——定义类继承结构
Jenkins learning (II) -- setting up Chinese
Go language - JSON processing
Vscode编辑器右键没有Open In Default Browser选项
Leetcode daily question (968. binary tree cameras)
QT qstring:: number apply base conversion
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
图像修复方法研究综述----论文笔记
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Flink学习笔记(十一)Table API 和 SQL
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?