当前位置:网站首页>剑指 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);
}
}
边栏推荐
- Halcon's practice based on shape template matching [1]
- Sql Server的存儲過程詳解
- Installation and use of libjpeg and ligpng
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- 实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
- My-basic application 1: introduction to my-basic parser
- QEMU demo makefile analysis
- STM32 --- serial port communication
- Tailq of linked list
- Introduction of air gap, etc
猜你喜欢
Classic application of MOS transistor circuit design (2) - switch circuit design
Charge pump boost principle - this article will give you a simple understanding
Brief discussion on Buck buck circuit
My-basic application 2: my-basic installation and operation
STM32 single chip microcomputer - bit band operation
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Introduction of air gap, etc
Various types of questions judged by prime numbers within 100 (C language)
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Summary of SIM card circuit knowledge
随机推荐
Ble encryption details
Use indent to format code
Volatile of C language
Carrier period, electrical speed, carrier period variation
Array integration initialization (C language)
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Consul installation
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
STM32 single chip microcomputer - external interrupt
H264 (I) i/p/b frame gop/idr/ and other parameters
Some thoughts on extracting perspectives from ealfa and Ebeta
General makefile (I) single C language compilation template
Explication de la procédure stockée pour SQL Server
NTC thermistor application - temperature measurement
Simple design description of MIC circuit of ECM mobile phone
Imx6ull bare metal development learning 2- use C language to light LED indicator
[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...)
Bluetooth hc-05 pairing process and precautions