当前位置:网站首页>剑指 Offer 05. 替换空格
剑指 Offer 05. 替换空格
2022-07-05 08:16:00 【程序员·小李】
题目:
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
限制:
0 <= s 的长度 <= 10000
思路:
1. 先进行一次遍历,查看空格的个数,空格需要替换成“%20”,因此需要对数组进行扩容。array.length - count + count * 3便是替换后数组的大小。
2. 从后向前依次复制。
class Solution {
public String replaceSpace(String s) {
if (s == null){
return null;
}
char[] array = s.toCharArray();
int count = 0;
for (char c : array){
if (c == ' '){
count++;
}
}
char[] newArray = new char[array.length - count + count * 3];
int pointer = newArray.length - 1;
for (int j = array.length - 1; j >= 0; j--){
if (array[j] != ' '){
newArray[pointer--] = array[j];
continue;
}
newArray[pointer--] = '0';
newArray[pointer--] = '2';
newArray[pointer--] = '%';
}
return new String(newArray);
}
}
边栏推荐
- Summary -st2.0 Hall angle estimation
- Talk about the function of magnetic beads in circuits
- Hardware and software solution of FPGA key chattering elimination
- Design a clock frequency division circuit that can be switched arbitrarily
- 实例009:暂停一秒输出
- Summary of SIM card circuit knowledge
- Fundamentals of C language
- DCDC circuit - function of bootstrap capacitor
- Solutions to compilation warnings in Quartus II
- C # joint configuration with Halcon
猜你喜欢

Circleq of linked list
![C WinForm [change the position of the form after running] - Practical Exercise 4](/img/f7/ddaf5773295ca6929d39d7aa760d36.jpg)
C WinForm [change the position of the form after running] - Practical Exercise 4
![[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement](/img/54/78e4e10724b2762274b06debb10877.jpg)
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement

Classic application of MOS transistor circuit design (1) -iic bidirectional level shift

NTC thermistor application - temperature measurement
![[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication](/img/08/7f28008a4aa999650998ba8dee5d8e.jpg)
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication

Hardware and software solution of FPGA key chattering elimination
![Halcon's practice based on shape template matching [2]](/img/70/3e905661785e570fb406b8e97d41e6.jpg)
Halcon's practice based on shape template matching [2]

C # joint configuration with Halcon

H264 (I) i/p/b frame gop/idr/ and other parameters
随机推荐
How to copy formatted notepad++ text?
Talk about the circuit use of TVs tube
Explain task scheduling based on Cortex-M3 in detail (Part 2)
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Why is 1900 not a leap year
Shell script realizes the reading of serial port and the parsing of message
NTC thermistor application - temperature measurement
H264 (I) i/p/b frame gop/idr/ and other parameters
Working principle and type selection of common mode inductor
Management and use of DokuWiki
General makefile (I) single C language compilation template
Solutions to compilation warnings in Quartus II
Bluetooth hc-05 pairing process and precautions
Stablq of linked list
C WinForm [realize the previous and next selection pictures] - practice 7
实例008:九九乘法表
MySQL之MHA高可用集群
Fundamentals of C language
Weidongshan Internet of things learning lesson 1
Network port usage