当前位置:网站首页>剑指 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;
}
}
边栏推荐
猜你喜欢
随机推荐
[turn]: Apache Felix framework configuration properties
Haut OJ 1352: string of choice
Solon Logging 插件的添加器级别控制和日志器的级别控制
[interval problem] 435 Non overlapping interval
Heap sort summary
xftp7与xshell7下载(官网)
质量体系建设之路的分分合合
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
Generate filled text and pictures
Development error notes
Reflection summary of Haut OJ freshmen on Wednesday
Romance of programmers on Valentine's Day
Acwing 4300. Two operations
kubeadm系列-02-kubelet的配置和启动
读者写者模型
Binary search basis
[turn]: OSGi specification in simple terms
Haut OJ 1243: simple mathematical problems
PMP考生,请查收7月PMP考试注意事项
Add level control and logger level control of Solon logging plug-in






![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)

