当前位置:网站首页>Leetcode——剑指 Offer 05. 替换空格
Leetcode——剑指 Offer 05. 替换空格
2022-07-07 12:15:00 【styfish】
概述
分析
这题,如果不限制空间复杂度,其实很简单,直接开一个新的
string
就可以实现了但是,如果要求只能原理处理呢?这里有一个问题需要解决:
s --> %20
,增加了字符,需要增加striing的容量因为
push_back
之能最末尾操作
思路
增加容量
- 可以先遍历一遍string,统计空格的个数,就可以知道最终需要扩充的大小了
- 然后利用
resize()
来扩充大小
代码
class Solution {
public:
string replaceSpace(string s) {
int count = 0; // 统计空格的个数
int sOldSize = s.size();
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
count++;
}
}
// 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小
s.resize(s.size() + count * 2);
// 为了原地操作,所以使用双指针法
int ptr = s.size() - 1, wokr_ptr = sOldSize - 1;
// 这里要从后往前,因为如果添加新元素从前往后,会覆盖掉原始的元素(
for(; wokr_ptr >= 0; --wokr_ptr) {
if (s[wokr_ptr] != ' ') s[ptr--] = s[wokr_ptr];
else {
s[ptr] = '0';
s[ptr - 1] = '2';
s[ptr - 2] = '%';
ptr -= 3;
}
}
return s;
}
};
边栏推荐
- Huawei image address
- THINKPHP框架的优秀开源系统推荐
- Supply chain supply and demand estimation - [time series]
- mysql ”Invalid use of null value“ 解决方法
- 参数关键字Final,Flags,Internal,映射关键字Internal
- VSCode 配置使用 PyLint 语法检查器
- GVIM [III] [u vimrc configuration]
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
- Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
猜你喜欢
Vmware 与主机之间传输文件
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
最长上升子序列模型 AcWing 1012. 友好城市
Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
Supply chain supply and demand estimation - [time series]
Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
Assign a dynamic value to the background color of DataGrid through ivalueconverter
Wired network IP address of VMware shared host
Parsing of XML files
Details of redis core data structure & new features of redis 6
随机推荐
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
PHP中用下划线开头的变量含义
Vscode configuration uses pylint syntax checker
c#利用 TCP 协议建立连接
Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
Hands on Teaching: XML modeling
Regular expression integer positive integer some basic expressions
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
3D Detection: 3D Box和点云 快速可视化
最长上升子序列模型 AcWing 482. 合唱队形
IP and long integer interchange
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
Horizontal of libsgm_ path_ Interpretation of aggregation program
Interface automation test - solution of data dependency between interfaces
The difference between memory overflow and memory leak
UML 状态图
Is it safe to open an account online now? Which securities company should I choose to open an account online?