当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 小程序直播+電商,想做新零售電商就用它吧!
- 使用Electron开发桌面应用
- Merge sort
- Add level control and logger level control of Solon logging plug-in
- Talking about JVM (frequent interview)
- Service fusing hystrix
- Hang wait lock vs spin lock (where both are used)
- What is the agile proportion of PMP Exam? Dispel doubts
- Haut OJ 1401: praise energy
- 浅谈JVM(面试常考)
猜你喜欢
随机推荐
软件测试 -- 0 序
Reverse one-way linked list of interview questions
Software test -- 0 sequence
Solon Auth 认证框架使用演示(更简单的认证框架)
Romance of programmers on Valentine's Day
Research on the value of background repeat of background tiling
Solon 框架如何方便获取每个请求的响应时间?
Transport connection management of TCP
Solon Logging 插件的添加器级别控制和日志器的级别控制
Magnifying glass effect
[allocation problem] 455 Distribute cookies
Drawing dynamic 3D circle with pure C language
支持多模多态 GBase 8c数据库持续创新重磅升级
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
GBase数据库助力湾区数字金融发展
Introduction to tools in TF-A
Reader writer model
High precision subtraction
To be continued] [UE4 notes] L4 object editing
Summary of Haut OJ 2021 freshman week
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)

![[depth first search] 695 Maximum area of the island](/img/08/cfff4aec667216e4f146205a12c13f.jpg)





