当前位置:网站首页>Sword finger offer question brushing record - offer 05 Replace spaces
Sword finger offer question brushing record - offer 05 Replace spaces
2022-07-19 07:09:00 【When to order】
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."
It is worth noting that ,java The string in is an immutable character sequence , Therefore, we need to find a way to change the content .
Method 1 : A character array ( Official answer )
Because every replacement from 1 Characters become 3 Characters , Using character arrays makes it easy to replace . Set the length of the character array to s The length of the 3 times , This ensures that the character array can hold all the replaced characters .
get s The length of length
Create a character array array, The length of length * 3
initialization size by 0,size Represents the length of the replaced string
Traversing the string from left to right s
get s The current character of c
If the characters c Is a space , Then order array[size] = '%',array[size + 1] = '2',array[size + 2] = '0', And will size The value of the add 3
If the characters c It's not a space , Then order array[size] = c, And will size The value of the add 1
After the traversal ,size The value of is equal to the length of the replaced string , from array Before size Create a new string of characters , And return the new string
public String replaceSpace(String s) {
int length = s.length();
char[] array = new char[length * 3];
int size = 0;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (c == ' ') {
array[size++] = '%';
array[size++] = '2';
array[size++] = '0';
} else {
array[size++] = c;
}
}
String newStr = new String(array, 0, size);
return newStr;
}
Method 2 : Use variable character sequences StringBuffer perhaps StringBuilder
Instantiate with constructor StringBuffer perhaps StringBuilder Pay attention to its capacity when , It is recommended to use with parameters new
StringBuffer(int capacity); among StringBuffer Class addition method is append(xxx)
public String replaceSpace1(String s){
StringBuffer sbuffer = new StringBuffer(s.length() * 3);
for (char s2 : s.toCharArray()){
if (s2 == ' ')
sbuffer.append("%20");
else
sbuffer.append(s2);
}
String s1 = new String(sbuffer);
return s1;
}Because the function needs to return a string type , There is String and StringBuffer Conversion between , Let's review it here :
public static void main(String[] args) {
12 //String -> StringBuffer
13 // Create a String object
14 String str = "Hi Java!";
15 System.out.println(str);
16
17 // Mode one : Construction method
18 StringBuffer buffer = new StringBuffer(str);
19 System.out.println(buffer);
20
21 // Mode two : adopt append Method
22 StringBuffer buffer2 = new StringBuffer();
23 buffer2.append(str);
24 System.out.println(buffer2);
25
26 //StringBuffer -> String
27 // Create a StringBuffer object
28 StringBuffer buffer3 = new StringBuffer();
29 buffer3.append("Happy birthday Java!");
30 System.out.println(buffer3);
31
32 // Mode one : By construction method
33 String str2 = new String(buffer3);
34 System.out.println(str2);
35
36 // Mode two : adopt toString Method
37 String str3 = buffer3.toString();
38 System.out.println(str3);
39 }Method 3 : Call directly replace( Tremble smart )
public String replaceSpace(String s) { String s1 = s.replace(" ", "%20"); return s1; }
Mutual encouragement !!
边栏推荐
- Speed feedback single closed loop DC speed regulation system based on Simulink
- CDN是什么?使用CDN有什么优势?
- m基于matlab的超宽带MIMO雷达对目标的检测仿真,考虑时间反转
- ARM服务器搭建 我的世界(MC) 1.18.2 版私服教程
- My world 1.18.1 forge version open service tutorial, can install mod, with panel
- 快速理解重定向
- Pytorch learning diary (II)
- Yuanzi racehorse.
- IP103.53.125.xxx IP地址段 详解
- 网站被劫持了怎么办?
猜你喜欢

PyTorch学习日记(四)

Minecraft bedrock BDS service tutorial

103.53.124.X IP段BGP线路和普通的专线有什么区别

论文阅读:Deep Residual Learning in Spiking Neural Networks

Speed feedback single closed loop DC speed regulation system based on Simulink

5G时代服务器在这里面起着什么作用?
Xiaodi network security notes - Information Collection - architecture, construction, WAF (8)

PyTorch学习笔记(一)

M FPGA implementation of chaotic digital secure communication system based on Lorenz chaotic self synchronization, Verilog programming implementation, with MATLAB chaotic program

华为云 鲲鹏ARM云服务器 和 x86云服务器 性能评测对比
随机推荐
IP103.53.125.xxx IP地址段 详解
M simulation of cooperative MIMO distributed space-time coding technology based on MATLAB
Performance test and price comparison of cloud servers of Alibaba cloud, Tencent cloud, Huawei cloud, ucloud and Tianyi cloud
Class and super, inheritance
怎么知道网络是否需要用高防服务器?怎么选择机房也是很重要的一点以及后期业务的稳定性
Data analysis and visualization -- the shoes with the highest sales volume on jd.com
爬虫基础—爬虫的基本原理
Yuanzi racehorse.
Legendary game setup tutorial
Xiaodi network security - Notes (4)
UCloud(优刻得) 上海 ARM 云服务器评测
快速掌握sort命令,tr命令
字典、元組和列錶的使用及區別,
How to set primary key self growth in PostgreSQL database
Pytorch learning diary (II)
剑指Offer刷题记录——Offer 06.从尾到头打印链表
Mingming loves drinking water
论文阅读:Deep Residual Learning in Spiking Neural Networks
Minecraft integration package [gtnh] gray Technology: new vision server building tutorial
【无标题】