当前位置:网站首页>字符串——剑指 Offer 05. 替换空格
字符串——剑指 Offer 05. 替换空格
2022-07-24 11:23:00 【向着百万年薪努力的小赵】
1 题目描述
剑指 Offer 05. 替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
2 题目示例
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
3 题目提示
0 <= s 的长度 <= 10000
4 思路
这道题的解法非常多,利用很多原生 apiapi 就能轻松解决,例如:
repleace/replaceAll
encodeURIComponent
split/join
亦或者直接暴破求解等等…
但是我们清楚,这些肯定都不是被考察的目的,我们需要做的,就是如何在手动实现的过程中,尽量的减少复杂度!
值得注意的是:数组遍历,一定要从后往前遍历,避免从前往后,造成字符被修改,导致错误!
由于每次替换从1个字符变成3个字符,使用字符数组可方便地进行替换。建立字符数组地长度为s的长度的3倍,这样可保证字符数组可以容纳所有替换后的字符。
- 获得s 的长度 1ength
- 创建字符数组array,其长度为1ength * 3
- 初始化size为0,size表示替换后的字符串的长度
- 从左到右遍历字符串s
- 获得s的当前字符c
- 如果字符c是空格,则令array[size] = ‘%’,array[size + 1] = '2’, array[size + 2] ='0’,并将size的值加3
- 如果字符C不是空格,则令array[size] = c,并将size的值加1
- 遍历结束之后,size的值等于替换后的字符串的长度,从array 的前size个字符创建新字符串,并返回新字符串
复杂性分析
时间复杂度:O(n)O(n)。遍历字符串 s 一遍。
空间复杂度:O(n)O(n)。额外创建字符数组,长度为 s 的长度的 3 倍。
5 我的答案
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;
}
}
边栏推荐
- CSDN会员的魅力何在?我要他有什么用?
- 07【Path、Files类的使用】
- 【Golang】golang实现md5加密函数
- 【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
- HDU5667 Sequence
- Decomposition of kubernets principle
- Why can't memset initialize array elements to 1?
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程
- Research on parameter setting of MATLAB FFT
- Publish local images to Alibaba cloud
猜你喜欢

【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)

Why can't memset initialize array elements to 1?

Paging query of employee information of black maredge takeout

这才是开发者神器正确的打开方式!

黑马瑞吉外卖之员工信息分页查询

Publish local image to private library

Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution
什么是云原生,云原生技术为什么这么火?

【10】 Teamwork and cross team collaboration

MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
随机推荐
Depth first search and breadth first search of Graphs
Pytorch learning -- using gradient descent method to realize univariate linear regression
【Golang】golang实现sha256加密函数
[deserialization vulnerability-01] Introduction to serialization and deserialization
Selenium automated test (this one is enough) - self study
黑马瑞吉外卖之员工信息分页查询
2022, the average salary of the soft tester, after reading it, I was instantly cool
FastCGI运行原理及php-fpm参数配置
Xilinx FPGA Microblaze AXI_ IIC usage and experience
What is the difference between low code and no code?
How to choose sentinel vs. hystrix current limiting?
JMeter if controller
Introduction to kubernetes Basics
[golang] golang implements the URLEncode URLDecode function
MySQL query field matches the record of a rule
运算放大器 —— 快速复苏笔记[壹](参数篇)
Idea background image set
Idea hidden Idea folder hides.Iml files
What is the charm of CSDN members? What's the use of him?
Leetcode 257. all paths of binary tree