当前位置:网站首页>String - Sword finger offer 05. replace spaces
String - Sword finger offer 05. replace spaces
2022-07-24 11:29:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
The finger of the sword Offer 05. Replace blank space
Please implement a function , Put the string s Replace each space in with "%20".
2 Title Example
Example 1:
Input :s = “We are happy.”
Output :“We%20are%20happy.”
3 Topic tips
0 <= s The length of <= 10000
4 Ideas
There are many solutions to this problem , Use many native apiapi It's easy to solve , for example :
repleace/replaceAll
encodeURIComponent
split/join
Or directly break the solution and so on …
But we know , These are certainly not the purpose of being investigated , What we need to do , That is, how to implement it manually , Minimize complexity !
It is worth noting that : Array traversal , Be sure to traverse from back to front , Avoid front to back , Cause characters to be modified , Cause error !
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 1ength
- Create a character array array, The length of 1ength * 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
Complexity analysis
Time complexity :O(n)O(n). Traversal string s Again .
Spatial complexity :O(n)O(n). Create extra character arrays , The length is s The length of the 3 times .
5 My answer
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;
}
}
边栏推荐
- How to use SSH and SFTP protocols at home
- Redis cluster setup
- Idea runs the wordcount program (detailed steps)
- 强引用、软引用、弱引用、虚引用有什么区别?
- Talk about software testing - automated testing framework
- 【Golang】golang实现urlencode urldecode函数
- [QNX Hypervisor 2.2用户手册]9.2 cmdline
- High frequency written test questions (Weilai)
- Exceptions about configuring Postgres parameters
- 高频笔试题(蔚来)
猜你喜欢

Fiddler packet capture tool summary

JMeter runtime controller

How to go from functional testing to automated testing?

Blue Bridge Cup provincial match training camp - Calculation of date

HCIP OSPF接口网络类型实验 第四天

What is the charm of CSDN members? What's the use of him?

HDU5667 Sequence

Sentinel vs Hystrix 限流对比,到底怎么选?

How to convert word to markdown text

E2PROM read / write (xiicps) on PS side of zcu102 board
随机推荐
What is the difference between strong reference, soft reference, weak reference and virtual reference?
What is cloud native? Why is cloud native technology so popular?
Fifty lectures of Euler (I)
JPS has no namenode and datanode reasons
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
[golang] golang实现截取字符串函数SubStr
哈希——18. 四数之和
Why can't memset initialize array elements to 1?
Depth first search and breadth first search of Graphs
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
Easy to understand ES6 (IV): template string
tcp 服务端接收数据处理思路梳理,以及select: Invalid argument报错 笔记
Altium one key automatic BOM
iMeta观点 | 短读长扩增子测序是否适用于微生物组功能的预测?
【10】 Teamwork and cross team collaboration
MySQL paging
链表——142. 环形链表 II
This is the right way for developers to open artifact!
Stream stream