当前位置:网站首页>剑指 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();
}
}
边栏推荐
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
- Conference Preview - Huawei 2012 lab global software technology summit - European session
- Analysis of KOA - onion model
- Openmldb meetup No.4 meeting minutes
- 移除无效的括号[用数组模拟栈]
- The website with id 0 that was requested wasn‘t found. Verify the website and try again
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set show in the point parameter_ The legend parameter is false, and the legend information is not displayed
- time 函数和 clock_gettime()函数的区别
- led背光板的作用是什麼呢?
- List集合
猜你喜欢
随机推荐
3D视觉检测在生产流水的应用有哪些
智慧法院新征程,无纸化办公,护航智慧法院绿色庭审
VScode选中多个单词
A high precision positioning approach for category support components with multiscale difference reading notes
The website with id 0 that was requested wasn‘t found. Verify the website and try again
redis在项目中的使用
STM32 移植 RT-Thread 标准版的 FinSH 组件
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set the alpha parameter in the point parameter to specify the transparency level of data points (points transparent
A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记
time 函数和 clock_gettime()函数的区别
List集合
Use of redis in projects
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
1020. 飞地的数量
Flutter 从零开始 005 图片及Icon
Shutter 007 input field from zero
Customize an annotation to get a link to the database
Quel est le rôle du rétroéclairage LED?
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
Map集合









