当前位置:网站首页>力扣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;
}
}边栏推荐
- go语言学习笔记三
- QT(39)-vs development qt program prompts that the source file cannot be opened
- L2-020 descendants of kung fu
- 光明区关于促进科技创新的若干措施(征求意见稿)
- Catch That Cow(详解)
- Simulation problem (middle)
- mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
- Learning of redis_Basic part
- 模拟问题(上)
- 七、自定义配置
猜你喜欢

Hexagon_V65_Programmers_Reference_Manual(14)

ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program

Small programs use npm packages to customize global styles

斐波那契数列的递归优化《备忘录递归》

mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")

Unity stepping on the pit record - the use of GetComponent

小程序npm包--API Promise化

Us to raise interest rates by 75 basis points in "technical recession"?Encryption market is recovering

IGBT wafers used in photovoltaic inverters

Discourse Custom Header Links
随机推荐
Whole process scheduling - Azkaban entry and advanced
Recursive Optimization of Fibonacci Sequences "Memo Recursion"
双指针问题(上)
LeetCode Algorithm 2326. Spiral Matrix IV
Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”
坪山区关于开展2022年度科技创新专项资金申报工作的通知
Kyligence 再获 CRN, insideBIGDATA 两大国际奖项认可
工具 | 常用 PostgreSQL 预防数据丢失方案
Stimulsoft ReportsJS and DashboardsJS. 2022.3.3
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
Seven, custom configuration
LeetCode Algorithm 2326. 螺旋矩阵 IV
gnss rtcm rtklib Ntrip...
光明区关于促进科技创新的若干措施(征求意见稿)
Xiamen SenseCore Technology MC3172(1): Introduction and Environment Construction
SVN View Username and Password
Learning of redis_Basic part
go language study notes 2
go语言学习笔记二
WPF study notes "WPF Layout Basics"