当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Drawing dynamic 3D circle with pure C language
- Use of room database
- Kali 2018 full image download
- 质量体系建设之路的分分合合
- 【ES实战】ES上的native realm安全方式使用
- Haut OJ 1221: a tired day
- 26、 File system API (device sharing between applications; directory and file API)
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- PMP考生,请查收7月PMP考试注意事项
- kubeadm系列-01-preflight究竟有多少check
猜你喜欢
随机推荐
[binary search] 34 Find the first and last positions of elements in a sorted array
FVP和Juno平台的Memory Layout介绍
kubeadm系列-00-overview
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Hang wait lock vs spin lock (where both are used)
Haut OJ 1218: maximum continuous sub segment sum
Transport connection management of TCP
Reflection summary of Haut OJ freshmen on Wednesday
PMP考生,请查收7月PMP考试注意事项
Grail layout and double wing layout
A new micro ORM open source framework
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Zzulioj 1673: b: clever characters???
远程升级怕截胡?详解FOTA安全升级
[to be continued] [UE4 notes] L1 create and configure items
浅谈JVM(面试常考)
Heap sort summary
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
[merge array] 88 merge two ordered arrays
Double pointer Foundation
![[to be continued] [UE4 notes] L1 create and configure items](/img/20/54ba719be2e51b7db5b7645b361e26.jpg)




![[转]MySQL操作实战(一):关键字 & 函数](/img/b1/8b843014f365b786e310718f669043.png)



