当前位置:网站首页>Sword finger offer 05 Replace spaces
Sword finger offer 05 Replace spaces
2022-07-05 08:21:00 【Programmer Xiao Li】
subject :
Please implement a function , Put the string s Replace each space in with "%20".
Example 1:
Input :s = "We are happy."
Output :"We%20are%20happy."
Limit :
0 <= s The length of <= 10000
Ideas :
1. Go through the first time , Check the number of spaces , Spaces need to be replaced with “%20”, Therefore, the array needs to be expanded .array.length - count + count * 3 Is the size of the array after replacement .
2. Copy from front to back .
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);
}
}
边栏推荐
- STM32 --- serial port communication
- C WinForm [help interface - send email] - practice five
- Solutions to compilation warnings in Quartus II
- Tailq of linked list
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- Management and use of DokuWiki (supplementary)
- [NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
- Stm32--- systick timer
- Negative pressure generation of buck-boost circuit
- Connection mode - bridge and net
猜你喜欢

Introduction of air gap, etc

Array integration initialization (C language)
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

C # joint configuration with Halcon

Let's briefly talk about the chips commonly used in mobile phones - OVP chips

Connection mode - bridge and net

STM32---IIC
![C WinForm [exit application] - practice 3](/img/25/30c795cc3fa6931eb1d733719d4ad0.jpg)
C WinForm [exit application] - practice 3

The firmware of the connected j-link does not support the following memory access
![Halcon's practice based on shape template matching [1]](/img/68/206eed7502fbf108a929aa9365b1ae.jpg)
Halcon's practice based on shape template matching [1]
随机推荐
Explain task scheduling based on Cortex-M3 in detail (Part 1)
动力电池UL2580测试项目包括哪些
MySQL之MHA高可用集群
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
The firmware of the connected j-link does not support the following memory access
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
Weidongshan Internet of things learning lesson 1
Some tips for using source insight (solve the problem of selecting all)
STM32 --- configuration of external interrupt
DokuWiki deployment notes
Measurement fitting based on Halcon learning [i] fuse Hdev routine
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Talk about the circuit use of TVs tube
STM32 outputs 1PPS with adjustable phase
Semiconductor devices (III) FET
STM32 --- NVIC interrupt
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
实例008:九九乘法表
DCDC circuit - function of bootstrap capacitor