当前位置:网站首页>7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
2022-07-28 05:08:00 【菜菜的大数据开发之路】
lt.剑指 Offer 05. 替换空格
[案例需求]

[思路分析一, 使用API]
使用StringBuilder, 不断遍历原有的字符串, 在sb.append过程中遇到空格时, 替换为%20即可;
[代码实现]
//使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制
public static String replaceSpace(StringBuffer str) {
if (str == null) {
return null;
}
//选用 StringBuilder 单线程使用,比较快,选不选都行
StringBuilder sb = new StringBuilder();
//使用 sb 逐个复制 str ,碰到空格则替换,否则直接复制
for (int i = 0; i < str.length(); i++) {
//str.charAt(i) 为 char 类型,为了比较需要将其转为和 " " 相同的字符串类型
//if (" ".equals(String.valueOf(str.charAt(i)))){
if (s.charAt(i) == ' ') {
sb.append("%20");
} else {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
或者
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();
}
}
[思路分析二, 不使用API]
创建字符数组, 然后遍历String中的每一个char字符,
注意这个字符数组长度-> 因为是一个空格要变成了三个字符(%20), 所以总长度最大为3倍原来的字符串长度(当然了如果除了空格之外还有其他元素, 所以不太可能用完)
[代码实现]
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-搜索二维矩阵 ||
[案例需求]
[思路分析]
[代码实现]
lt.240-搜索二维矩阵 ||
[案例需求]
[思路分析]
[代码实现]
边栏推荐
- Interpreting the source code of cfrunloopref
- 驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
- HDU 2586 How far away ? (LCA multiplication method)
- FreeRTOS learning (I)
- From the basic concept of micro services to core components - explain and analyze through an example
- Supervisor series: 5. Log
- I've been in an outsourcing company for two years, and I feel like I'm going to die
- HDU 1435 stable match
- Dynamic SQL and paging
- 【CPU占用高】software_reporter_tool.exe
猜你喜欢

Table image extraction based on traditional intersection method and Tesseract OCR

【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution
![(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection

Dcgan:deep volume general adaptive networks -- paper analysis

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

What should testers know about login security?

Why is MD5 irreversible, but it may also be decrypted by MD5 free decryption website

RT based_ Distributed wireless temperature monitoring system based on thread

Keil Chinese garbled code solution

猿辅导技术进化论:助力教与学 构想未来学校
随机推荐
UI automation test farewell from now on, manual download browser driver, recommended collection
Inspire domestic students to learn robot programming education for children
HDU 1530 maximum clique
Interpreting the source code of cfrunloopref
【CPU占用高】software_reporter_tool.exe
Win10 machine learning environment construction pycharm, anaconda, pytorch
[daily question 1] 735. Planetary collision
Automated test tool playwright (quick start)
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
【CVPR2022】On the Integration of Self-Attention and Convolution
【ARIXV2204】Neighborhood attention transformer
After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
Research on the design of robot education in stem course
Testcafe's positioning, operation of page elements, and verification of execution results
The research group passed the thesis defense successfully
Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
Making RPM packages with nfpm
list indices must be integers or slices, not tuple
go-zero单体服务使用泛型简化注册Handler路由
Message forwarding mechanism -- save your program from crashing