当前位置:网站首页>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 !!
边栏推荐
- regular expression
- 【无标题】
- Yuanzi racehorse.
- Tianyi cloud Hangzhou virtual machine (VPS) performance evaluation
- Poor Xiaofan (simulation)
- 我的世界 1.18.1 Forge版 开服教程,可装MOD,带面板
- How to open the service of legendary mobile games? How much investment is needed? What do you need?
- Closures and decorators
- Data protection / disk array raid protection IP segment 103.103.188 xxx
- What do you need to build a website
猜你喜欢

Review summary of MySQL

m基于matlab的BTS天线设计,带GUI界面

m基于simulink的16QAM和2DPSK通信链路仿真,并通过matlab调用simulink模型得到误码率曲线

Steam游戏服务器配置选择 IP

103.53.124. What is the difference between X IP BGP line and ordinary dedicated line

About file upload and download

PyTorch学习笔记(一)

Arm server building my world (MC) version 1.18.2 private server tutorial

正则表达式

基于simulink的转速反馈单闭环直流调速系统
随机推荐
数据保护/磁盘列阵RAID保护 IP段103.103.188.xxx
Mingming loves drinking water
m基于simulink的16QAM和2DPSK通信链路仿真,并通过matlab调用simulink模型得到误码率曲线
字典、元組和列錶的使用及區別,
闭包与装饰器
Solve the problem that the unit test coverage of sonar will be 0
Mapping rule configuration of zuul route
Tower of Hanoi 2 (function)
regular expression
数据分析及可视化——京东上销量最高的鞋子
Dictionary, use of sets, conversion of data types
IP fragment是什么意思?如何防御IP fragment攻击?
pytorch张量
PyTorch学习日记(二)
UCloud(优刻得) 上海 ARM 云服务器评测
urllib库的使用
How to set primary key self growth in PostgreSQL database
怎么知道网络是否需要用高防服务器?怎么选择机房也是很重要的一点以及后期业务的稳定性
Debug wechat one hop under linxu (Fedora 27)
CDN是什么?使用CDN有什么优势?