当前位置:网站首页>Sword finger offer05 Replace spaces
Sword finger offer05 Replace spaces
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer05. Replace blank space
Please implement a function , Put the string s Replace each space in with "%20".
Example 1:
Input :s = "We are happy."
Output :"We%20are%20happy."
Limit :
0 <= s The length of <= 10000
JAVA Code
Tool class methods
Here's a list of , But you can't take advantage of opportunism ~~
class Solution {
public String replaceSpace(String s) {
return s.replace(" ","%20");
}
}
The official way
By converting a string into a character array , Analyze the string character by character , If it is a space, insert it into the array % 2 0 Three characters , If it is not a space, insert the position character directly into the array .
Be careful : Not directly arr.toString() To string , It's messy .
new String( Source array , The starting position , Termination position )
class Solution {
public String replaceSpace(String s) {
char[] arr = new char[s.length()*3];
int index = 0;
for(int i = 0;i<s.length();i++){
if(s.charAt(i)==' '){
arr[index++] = '%';
arr[index++] = '2';
arr[index++] = '0';
}else{
arr[index++] = s.charAt(i);
}
}
String newStr = new String(arr,0,index);
return newStr;
}
}
The idea of solving problems is very important .
边栏推荐
猜你喜欢
C language improvement article (wchar_t) character type
Unicode encoding table download
云计算未来 — 云原生
OpenGL index cache object EBO and lineweight mode
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
(construction notes) ADT and OOP
TOGAF认证自学宝典V2.0
Cloud Computing future - native Cloud
剑指Offer07. 重建二叉树
Shutter: add gradient stroke to font
随机推荐
elastic_ L01_ summary
Jsup crawls Baidu Encyclopedia
Use of QT OpenGL camera
elastic_ L04_ introduction. md
Display time with message interval of more than 1 minute in wechat applet discussion area
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
2.6 preliminary cognition of synergetic couroutines
(构造笔记)GRASP学习心得
How to deploy web pages to Alibaba cloud
Applet wxss introduction
239. Sliding window maximum
写一个简单的nodejs脚本
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
(construction notes) learning experience of MIT reading
Shell: basic learning
Shutter: about inheritedwidget
How to convert a numeric string to an integer
Basic knowledge of OpenGL (sort it out according to your own understanding)
Flutter Widget : KeyedSubtree
在网上炒股开户可以吗?资金安全吗?