当前位置:网站首页>剑指 Offer 05. 替换空格: 把字符串 s 中的每个空格替换成“%20“
剑指 Offer 05. 替换空格: 把字符串 s 中的每个空格替换成“%20“
2022-06-30 11:57:00 【网小鱼的学习笔记】
解法1:API直接用一下
class Solution {
public String replaceSpace(String s) {
s = s.replace(" ", "%20");
return s;
}
}
面试官:API 用的很好,回去等通知吧
解法2:
算法流程:
- 初始化一个 list (Python) / StringBuilder (Java) ,记为 res ;
- 遍历列表 s 中的每个字符 c :
当 c 为空格时:向 res 后添加字符串 “%20” ;
当 c 不为空格时:向 res 后添加字符 c ; - 将列表 res 转化为字符串并返回。
public class ReplaceBlank05 {
public static void main(String[] args) {
String s = replaceSpace("We are happy.");
System.out.println(s);
}
public static String replaceSpace(String s) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' '){
stringBuilder.append("%20");
}else{
stringBuilder.append(s.charAt(i));
}
}
return stringBuilder.toString();
}
}
边栏推荐
- Using cookie technology to realize historical browsing records and control the number of displays
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ The size function configures the measurement adjustment range of the size of the data point
- [cf] 803 div2 A. XOR Mixup
- 论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
- Talk about how to do hardware compatibility testing and quickly migrate to openeuler?
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
- AutoCAD - len command
- Map collection
- AGCO AI frontier promotion (6.30)
- Embedded sig | multi OS hybrid deployment framework
猜你喜欢
随机推荐
【BUG解决】fiftyone报AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘错误解决方法
Object mapping - mapping Mapster
1254. 统计封闭岛屿的数目
实现多方数据安全共享,解决普惠金融信息不对称难题
Embedded sig | multi OS hybrid deployment framework
Set集合
服务器常用的一些硬件信息(不断更新)
[cf] 803 div2 B. Rising Sand
TypeScript ReadonlyArray(只读数组类型) 详细介绍
这些电影中的科幻构想,已经用AI实现了
麒麟软件韩乃平:数字中国建设需要自己的开源根社区
OpenMLDB Meetup No.4 会议纪要
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
Use of redis in projects
DMA controller 8237a
led背光板的作用是什么呢?
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ x_ The log10 function configures the value range of the X axis to be logarithmic coordinates
MySQL 表的内连和外连
A high precision positioning approach for category support components with multiscale difference reading notes
![移除无效的括号[用数组模拟栈]](/img/df/0a2ae5ae40adb833d52b2dddea291b.png)





