当前位置:网站首页>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();
}
}
边栏推荐
- MATLAB中polarplot函数使用
- Serial communication interface 8250
- Getting started with the go language is simple: go handles XML files
- 立创 EDA #学习笔记10# | 常用连接器元器件识别 和 无源蜂鸣器驱动电路
- Map集合
- A high precision positioning approach for category support components with multiscale difference reading notes
- 3D视觉检测在生产流水的应用有哪些
- Swagger2自动生成APi文档
- Joplin实现样式更改
- 【LeetCode】15、三数之和
猜你喜欢

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

使用深度学习进行生物网络分析

redis在项目中的使用

Map集合

SuperMap 3D SDKs_Unity插件开发——连接数据服务进行SQL查询
![移除无效的括号[用数组模拟栈]](/img/df/0a2ae5ae40adb833d52b2dddea291b.png)
移除无效的括号[用数组模拟栈]

Use of redis in projects

A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记

海思3559万能平台搭建:YUV格式简介

Edusoho enterprise training version intranet only deployment tutorial (to solve the problems of player, upload and background jam)
随机推荐
200. 岛屿数量
Joplin实现样式更改
Subtrate 源码追新导读-5月上旬: XCM 正式启用
[cf] 803 div2 B. Rising Sand
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
DMA控制器8237A
SuperMap 3D SDKs_Unity插件开发——连接数据服务进行SQL查询
VScode选中多个单词
Boost study: boost log
Vscode select multiple words
What are the applications of 3D visual inspection in production flow
Ensemble de cartes
1020. 飞地的数量
[leetcode] 15. Sum of three numbers
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
YOLOv5导出onnx遇到的坑
Redis6 learning notes - Chapter 2 - Basic redis6 operations
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
海思3559万能平台搭建:获取数据帧修改后编码
Shutter start from zero 006 radio switches and checkboxes