当前位置:网站首页>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-搜索二维矩阵 ||
[案例需求]
[思路分析]
[代码实现]
边栏推荐
- 【CVPR2022】On the Integration of Self-Attention and Convolution
- Keil Chinese garbled code solution
- HashSet add
- How to quickly turn function test to automatic test
- How does Alibaba use DDD to split microservices?
- 使用nfpm制作rpm包
- POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
- HashSet add
- Win10 machine learning environment construction pycharm, anaconda, pytorch
- Interview fraud: there are companies that make money from interviews
猜你喜欢

Check box error

The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight

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?

Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"

RT_ Use of thread message queue
![(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

How to send and receive reports through outlook in FastReport VCL?

Improve the core quality of steam education among students

Special topic of APP performance design and Optimization - poor implementation affecting performance

Keil Chinese garbled code solution
随机推荐
I've been in an outsourcing company for two years, and I feel like I'm going to die
Data security is gradually implemented, and we must pay close attention to the source of leakage
How to quickly locate bugs? How to write test cases?
FreeRTOS startup process, coding style and debugging method
[Hongke technology] Application of network Multimeter in data center
【ARXIV2205】Inception Transformer
Do you know several assertion methods commonly used by JMeter?
Can plastics comply with gb/t 2408 - Determination of flammability
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
Service object creation and use
Paper reading notes -- crop yield prediction using deep neural networks
HDU 1435 stable match
The research group passed the thesis defense successfully
Redis配置文件详解/参数详解及淘汰策略
Data imbalance: comprehensive sampling of anti fraud model (data imbalance)
Implementation of simple upload function in PHP development
How to quickly turn function test to automatic test
MySQL(5)
【CPU占用高】software_reporter_tool.exe