当前位置:网站首页>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
- Shell: basic learning
- (構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
- Develop plug-ins for idea
- (构造笔记)ADT与OOP
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- 手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- (构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
- elastic_ L01_ summary
猜你喜欢
随机推荐
The difference between lambda and anonymous inner class
232. Implement queue with stack
111. Minimum depth of binary tree
225. Implement stack with queue
Unicode查询的官方网站
Implement verification code verification
剑指Offer04. 二维数组中的查找【中等】
Summary of development issues
在网上炒股开户可以吗?资金安全吗?
Atomic atomic operation
Integer int compare size
Redis
2.9 overview of databinding knowledge points
Jsup crawls Baidu Encyclopedia
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Adult adult adult
Cloud Computing future - native Cloud
Shardingsphere sub database and sub table < 3 >
Integer string int mutual conversion









