当前位置:网站首页>Sword finger offer 05 Replace spaces: replace each space in the string s with "%20"“
Sword finger offer 05 Replace spaces: replace each space in the string s with "%20"“
2022-06-30 12:17:00 【Learning notes of net fish】
solution 1:API Just use it
class Solution {
public String replaceSpace(String s) {
s = s.replace(" ", "%20");
return s;
}
}
interviewer :API It works very well , Go back and wait for the announcement
solution 2:
Algorithm flow :
- Initialize a list (Python) / StringBuilder (Java) , Write it down as res ;
- Traverse the list s Each character in c :
When c When is a space : towards res Add string after “%20” ;
When c When not a space : towards res Add characters after c ; - Will list res Convert to a string and return .
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_ The size function configures the measurement adjustment range of the size of the data point
- 使用Power Designer工具构建数据库模型
- Statistics on the number of closed Islands
- beego开发博客系统学习(二)
- AGCO AI frontier promotion (6.30)
- The sci-fi ideas in these movies have been realized by AI
- After the node is installed in the NVM, the display is not internal or external when the NPM instruction is used
- 剑指 Offer 05. 替换空格: 把字符串 s 中的每个空格替换成“%20“
- 构造函数、类成员、析构函数调用顺序
- A high precision positioning approach for category support components with multiscale difference reading notes
猜你喜欢

海思3559开发常识储备:相关名词全解

Analysis of KOA - onion model

Map集合

这些电影中的科幻构想,已经用AI实现了

Embedded SIG | 多 OS 混合部署框架

A high precision positioning approach for category support components with multiscale difference reading notes

使用Power Designer工具构建数据库模型

立创 EDA #学习笔记10# | 常用连接器元器件识别 和 无源蜂鸣器驱动电路

海思3559 sample解析:venc
![Remove invalid parentheses [simulate stack with array]](/img/df/0a2ae5ae40adb833d52b2dddea291b.png)
Remove invalid parentheses [simulate stack with array]
随机推荐
[cf] 803 div2 B. Rising Sand
NoSQL——Redis的配置与优化
qt msvc 安装及调试
Talk about how to do hardware compatibility testing and quickly migrate to openeuler?
The sci-fi ideas in these movies have been realized by AI
服务器常用的一些硬件信息(不断更新)
Swagger2自动生成APi文档
Serial communication interface 8250
List集合
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
Flutter 从零开始 006 单选开关和复选框
Shutter 007 input field from zero
[cf] 803 div2 A. XOR Mixup
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記
[cf] 803 div2 B. Rising Sand
A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记
使用Power Designer工具构建数据库模型
麒麟软件韩乃平:数字中国建设需要自己的开源根社区
不同类型的变量与零究竟是如何比较
海思3559开发常识储备:相关名词全解