当前位置:网站首页>剑指 Offer 05. 替换空格
剑指 Offer 05. 替换空格
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述
问题解析
1.使用数组来做,首先将新建一个数组,计算原始字符串的长度当做数组长度
2.遍历数组。找到空格字符串,提换为“%20”;
3.新数组建立原来数组长度的三倍,用来存放新字符串
4.字符串数组转为字符串
5.考察.charAt的用法,以及字符数组转为字符串的用法(String a =new String())
代码实例
class Solution {
public String replaceSpace(String s) {
int len = s.length();
int[] s1 = new int[3*n];
int j = 0;
for(int i = 0;i < n;i++){
if(s.charAt(i) == ' '){
s1[j++] = "%";
s1[j++] = "2";
s1[j++] = "0";
}
else{
s1[j++] = s.chaAt(i);
}
}
String newStr = new String(s1,0,j);
return newStr;
}
}
边栏推荐
- Haut OJ 1221: a tired day
- Developing desktop applications with electron
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- BUUCTF MISC
- Yolov5 ajouter un mécanisme d'attention
- 游戏商城毕业设计
- [trans]: spécification osgi
- Development error notes
- Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
- [转]MySQL操作实战(一):关键字 & 函数
猜你喜欢
服务熔断 Hystrix
[to be continued] [UE4 notes] L3 import resources and project migration
[转]: OSGI规范 深入浅出
sync. Interpretation of mutex source code
Learning notes of "hands on learning in depth"
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
To be continued] [UE4 notes] L4 object editing
用STM32点个灯
利用HashMap实现简单缓存
[to be continued] [UE4 notes] L2 interface introduction
随机推荐
kubeadm系列-01-preflight究竟有多少check
TF-A中的工具介绍
room数据库的使用
Page countdown
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
[merge array] 88 merge two ordered arrays
C language Essay 1
Grail layout and double wing layout
Acwing 4300. Two operations
小程序直播+電商,想做新零售電商就用它吧!
Haut OJ 1401: praise energy
Zzulioj 1673: b: clever characters???
Haut OJ 1347: addition of choice -- high progress addition
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Pointnet++的改进
使用命令符关闭笔记本自带键盘命令
Es module and commonjs learning notes -- ESM and CJS used in nodejs
Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
Solon 框架如何方便获取每个请求的响应时间?
Es module and commonjs learning notes