当前位置:网站首页>剑指 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;
}
}
边栏推荐
猜你喜欢
随机推荐
A preliminary study of sdei - see the essence through transactions
Transport connection management of TCP
用STM32点个灯
National teacher qualification examination in the first half of 2022
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Download xftp7 and xshell7 (official website)
YOLOv5添加注意力机制
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
MySQL数据库(一)
[转]: OSGI规范 深入浅出
Solon Logging 插件的添加器级别控制和日志器的级别控制
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Hang wait lock vs spin lock (where both are used)
[turn]: OSGi specification in simple terms
SAP-修改系统表数据的方法
[to be continued] [UE4 notes] L2 interface introduction
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
Count sort
Pointnet++的改进




![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)



