当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
- Fragment addition failed error lookup
- 质量体系建设之路的分分合合
- Zzulioj 1673: b: clever characters???
- Haut OJ 1350: choice sends candy
- Reverse one-way linked list of interview questions
- SAP-修改系统表数据的方法
- [转]MySQL操作实战(一):关键字 & 函数
- [es practice] use the native realm security mode on es
- The present is a gift from heaven -- a film review of the journey of the soul
猜你喜欢
随机推荐
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Web APIs DOM node
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
SAP method of modifying system table data
Use the command character to close the keyboard command of the notebook
Pointnet++的改进
The next key of win generates the timestamp file of the current day
Web APIs DOM节点
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Simple HelloWorld color change
Heap sort summary
Count sort
PMP考试敏捷占比有多少?解疑
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Quick sort summary
Merge sort
How can the Solon framework easily obtain the response time of each request?
Solon Auth 认证框架使用演示(更简单的认证框架)
Add level control and logger level control of Solon logging plug-in