[CareerCup] 1.4 Replace Spaces 替换空格
替换 1.4 空格 replace CareerCup
2023-09-11 14:21:39 时间
1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
这道题让我们将一个字符串里所有的空格都替换为'%20',而且说明最好用in place的方法,就是说不要建新的字符串。如果建个新的字符,那这题解法就显得略low,因为那样我们只需要遍历原字符串s,遇到字符直接加进来,遇到空格,直接加个'%20'就行了。这道题要使用高大上的in place的方法,那就是我们先便利一遍给定字符串s,统计空格的个数,然后再resize一下字符串,增大字符串s的容量,以便我们来替换。然后我们再从原来s的末尾位置向开头遍历,遇到空格,在末尾三个空添加'%20',然后标示新位置,遇到非空格字符,则添加该字符,然后再标示新位置即可。代码如下:
class Solution { public: void replaceSpaces(string &s) { int count = 0, len = s.size(), newLen = 0; for (int i = 0; i < len; ++i) { if (s[i] == ' ') ++count; } newLen = len + 2 * count; s.resize(newLen); for (int i = len - 1; i >= 0; --i) { if (s[i] == ' ') { s[newLen - 1] = '0'; s[newLen - 2] = '2'; s[newLen - 3] = '%'; newLen -= 3; } else { s[newLen - 1] = s[i]; newLen -= 1; } } } };
相关文章
- 用以替换系统NSLog的YouXianMingLog
- js字符串替换
- Windows下如何替换鼠标指针
- UE查找和替换技巧实例
- url 中非法字符替换,java 正则替换
- Shell学习笔记---变量的赋值与替换
- linux(centos8):用tr替换或删除字符
- 使用lodash替换js字符串中的变量
- [Shell] Bash变量:变量测试与内容替换
- Python:re.sub()实现字符串替换
- vi 替换操作
- 5.IDA-文本搜索、二进制搜索(16进制字节序列)、替换16进制
- SQL Sever — 查询前10%的数据、查询并参与计算显示出数据、查询前10行数据、多重升序降序查询、去掉重复值、替换列名显示
- Ansible 常用模块之文件操作查找与内容替换 find|replace