当前位置:网站首页>力扣05-替换空格——字符串问题
力扣05-替换空格——字符串问题
2022-07-30 05:09:00 【张怼怼√】
问题描述
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
问题求解
可以新建一个StringBuffer或者StringBuilder,然后遍历原来的string,如果遇到空格则append“%20”,否则就将charAt(i) append进去。
这道题目还可以用双指针法求解,但题目过于简单就不做了,能通过就好。
输入输出示例

代码
class Solution {
public String replaceSpace(String s) {
StringBuffer str = new StringBuffer();
int len = s.length();
char ch;
for(int i = 0; i < len; i++){
ch = s.charAt(i);
if(ch == ' '){
str.append("%20");
}else{
str.append(ch);
}
}
String str1 = str.toString();
return str1;
}
}边栏推荐
- 容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群
- ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
- Web page element parsing a tag
- 4. Web Development
- QT(39)-vs development qt program prompts that the source file cannot be opened
- [3D Detection Series-PointRCNN] Reproduces the PointRCNN code, and realizes the visualization of PointRCNN3D target detection, including the download link of pre-training weights (starting from 0 and
- 罗湖区工匠技能领军人才奖励项目申请指南
- GO语言学习笔记一
- L2-025 分而治之
- Naive Bayes Classification
猜你喜欢

Summary of skills in using ms project2010 project management software

动态规划问题(完结篇)

The Complete Go Books - Beginner to Advanced and Web Development

美国再次加息75个基点 陷入“技术性衰退”?加密市场却呈现复苏力量

Stimulsoft ReportsJS and DashboardsJS. 2022.3.3

Compound Types--references, pointers

WPF introduces ttf icon file usage record
![[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset](/img/f2/429e3dd157647bb614595a83843140.png)
[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset

五、视图解析与模板引擎

IIS网站出现401未授权访问
随机推荐
【Vitis】ZCU102开发板PS端控制PL端复位的代码实现
(Hexagon_V65_Programmers_Reference_Manual(13)
5. View parsing and template engine
go language study notes 4
4. Web Development
模拟问题(下)
1315_使用LOOPBACK模拟模式pyserial安装是否成功的测试
Code readability, pre-checks, comments and summaries
nSoftware.PowerShell.Server.2020
模拟问题(中)
Double pointer problem (middle)
DLL说明(1)
handler+message [message mechanism]
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
std::vector中保存指针时用法
Catch That Cow (detailed)
Small program npm package--API Promise
SaaS多租户数据隔离的三种解决方案
Requirements design document and the changing role of the product manager
容器化|在 S3 备份恢复 RadonDB MySQL 集群数据