当前位置:网站首页>7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
2022-07-28 05:12:00 【Caicai's big data development path】
lt. The finger of the sword Offer 05. Replace blank space
[ Case needs ]

[ Train of thought analysis 1 , Use API]
Use StringBuilder, Continue to traverse the original string , stay sb.append When spaces are encountered in the process , Replace with %20 that will do ;
[ Code implementation ]
// Use a new object , Copy str, The process of copying is judged , If it is a space, replace , Otherwise, copy directly , Similar to array copy
public static String replaceSpace(StringBuffer str) {
if (str == null) {
return null;
}
// choose StringBuilder Single thread use , Faster , Choose or not
StringBuilder sb = new StringBuilder();
// Use sb Copy one by one str , If you encounter a space, replace , Otherwise, copy directly
for (int i = 0; i < str.length(); i++) {
//str.charAt(i) by char type , Turn it into a need for comparison " " Same string type
//if (" ".equals(String.valueOf(str.charAt(i)))){
if (s.charAt(i) == ' ') {
sb.append("%20");
} else {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
perhaps
class Solution {
public String replaceSpace(String s) {
//
StringBuilder sb = new StringBuilder();
for(Character c : s.toCharArray()){
if(c == ' '){
sb.append("%20");
continue;
}
sb.append(c);
}
return sb.toString();
}
}
[ Train of thought analysis II , Don't use API]
Create a character array , Then traverse String Each of them char character ,
Note the length of this character array -> Because a space becomes three characters (%20), So the total lengthThe maximum is 3 Times the original string length( Of course, if there are other elements besides spaces , So it's unlikely to run out )
[ Code implementation ]
class Solution {
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;
}
}

lt.240- Search for a two-dimensional matrix ||
[ Case needs ]
[ Thought analysis ]
[ Code implementation ]
lt.240- Search for a two-dimensional matrix ||
[ Case needs ]
[ Thought analysis ]
[ Code implementation ]
边栏推荐
- The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
- FreeRTOS personal notes - task notification
- POJ 1330 Nearest Common Ancestors (lca)
- 7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
- Applet import project
- Keil Chinese garbled code solution
- With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?
- [learning record] data enhancement 1
- Mysql基本查询
- How to quickly turn function test to automatic test
猜你喜欢

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

Do you know several assertion methods commonly used by JMeter?

在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子

基于MPLS构建虚拟专网的配置实验

RT_ Use of thread mailbox

【ARXIV2203】SepViT: Separable Vision Transformer

Improve the core quality of steam education among students

【ARIXV2204】Neighborhood attention transformer

The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?

Offline loading of wkwebview and problems encountered
随机推荐
Offline loading of wkwebview and problems encountered
使用nfpm制作rpm包
【计算机三级信息安全】信息安全保障概述
Dcgan:deep volume general adaptive networks -- paper analysis
RT_ Use of thread mailbox
【CVPR2022】On the Integration of Self-Attention and Convolution
Pipe /createpipe
What is the core value of testing?
数据库日期类型全部为0
HDU 3585 maximum shortest distance
FPGA: use PWM wave to control LED brightness
How to simulate common web application operations when using testcafe
UI automation test farewell from now on, manual download browser driver, recommended collection
Database date types are all 0
塑料可以执行GB/T 2408 -燃烧性能的测定吗
How to quickly turn function test to automatic test
Can plastics comply with gb/t 2408 - Determination of flammability
Check box error
【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers
flink思维导图