当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- Under the national teacher qualification certificate in the first half of 2022
- Grail layout and double wing layout
- Use of snippets in vscode (code template)
- [trans]: spécification osgi
- [to be continued] [UE4 notes] L2 interface introduction
- SAP-修改系统表数据的方法
- A new micro ORM open source framework
- [轉]: OSGI規範 深入淺出
- Service fusing hystrix
猜你喜欢
随机推荐
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
用STM32点个灯
The present is a gift from heaven -- a film review of the journey of the soul
YOLOv5-Shufflenetv2
浅谈JVM(面试常考)
room数据库的使用
Chapter 6 data flow modeling - after class exercises
Solon 框架如何方便获取每个请求的响应时间?
TF-A中的工具介绍
To the distance we have been looking for -- film review of "flying house journey"
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Zzulioj 1673: b: clever characters???
[binary search] 69 Square root of X
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
Es module and commonjs learning notes -- ESM and CJS used in nodejs
Acwing 4300. Two operations
Csp-j-2020-excellent split multiple solutions
Reflection summary of Haut OJ freshmen on Wednesday
Learning notes of "hands on learning in depth"
A new micro ORM open source framework