当前位置:网站首页>Replace spaces in niuke.com
Replace spaces in niuke.com
2022-07-22 17:51:00 【Lazy future】
List of articles
One . subject
1. subject
Please implement a function , Replace each space in a string with
“%20”
. for example , When the string isWe Are Happy.
The replaced string isWe%20Are%20Happy
2. Basic framework
- C++ Code framework of version
class Solution {
public:
void replaceSpace(char *str,int length) {
}
};
3. Original link
Two . Their thinking
1. Thought analysis
( 1 ) (1) (1) Only with the help of s t r str str The string pointed to , Move from back to front ;
( 2 ) (2) (2) Replace a space with '%''2''0'
, Replace one character with three characters , It's equivalent to an increase in 2 Characters ;
( 3 ) (3) (3) A loop counts the number of spaces in the string , The length of the replaced string is the length of the original string plus the number of cells 2 times .
( 4 ) (4) (4) Two variables e n d 1 、 e n d 2 end1、end2 end1、end2 Record the subscript of the last character of the original string and the subscript of the last character of the new string respectively ;
( 5 ) (5) (5) A cycle , If the subscript e n d 1 end1 end1 The character of is not a space , Just put e n d 1 end1 end1 The subscript character moves to e n d 2 end2 end2 Subscript location , Then both subscripts are subtracted 1;
( 6 ) (6) (6) If the subscript e n d 1 end1 end1 The characters are spaces , e n d 1 end1 end1 reduce 1,, hold '0'、'2'、'%'
These three characters are stored in subscript e n d 2 end2 end2 Location , After each deposit e n d − − end-- end−−;
( 7 ) (7) (7) When the space replacement is complete ,end1 And end2 equal , End of cycle .
2. Code details
Ideas 1
class Solution {
public:
void replaceSpace(char *str,int length) {
// Number of spaces found
int cnt = 0;
char* start = str;
while(*start){
if(*start == ' '){
cnt++;
}
start++;
}
// Move from back to front , Space encountered
// Before and after subscript
int end1 = length - 1;
int end2 = length - 1 + 2*cnt;
while(end1 != end2){
if(str[end1] != ' '){
str[end2--] = str[end1--];
}
else{
str[end2--] = '0';
str[end2--] = '2';
str[end2--] = '%';
end1--;
}
}
}
};
Ideas 2
class Solution {
public:
void replaceSpace(char *str,int length) {
// Find the number of spaces
char* start = str;
int cnt = 0;
while (*start) {
if (*start == ' ') {
cnt++;
}
start++;
}
int len1 = length;
int len2 = length + 2 * cnt;
// Create an array to store new strings
char* s = (char*)malloc(sizeof(char) * (len2+1));
start = str;
char* ps = s;
while (*start) {
if (*start != ' ') {
*ps++ = *start++;
}
else {
start++;
*ps++ = '%';
*ps++ = '2';
*ps++ = '0';
}
}
*ps = '\0';
strcpy(str, s);
free(s);
s = NULL;
}
};
3、 ... and . Knowledge and harvest of this topic
Ontology has two ideas : Ontology has two ideas : Ontology has two ideas :
The first method is to put it from back to front , There is no need to create a new array . The first method is to put it from back to front , There is no need to create a new array . The first method is to put it from back to front , There is no need to create a new array .
The second is the conventional method , From front to back . Create a new character array , Put the contents of the original string into the created character array in turn , If you encounter a space character, put three characters ‘%’、‘2’、‘0’
To character array , Until the end of the string . Then copy the contents of the character array back to the original string .
E N D END END
边栏推荐
- 如何一边看全景一边预约点餐?VR餐饮系统教程来了
- Can the task of flinksql monitor whether it is interrupted?
- [must see for developers] [push kit] collection of typical problems of push service 1
- Intent 跳转 传递list集合
- [MCU simulation project] external interrupts 0 and 1 control diodes through different triggering methods respectively
- Ajout, suppression et modification de MySQL (niveau avancé)
- Take CRM system as an example to explain data analysis (importance introduction and analysis method)
- Virtual machine performance test scheme
- ORACLE语句调整
- [database] addition, deletion, modification and query of MySQL table (Advanced)
猜你喜欢
[external sorting] merge ideas to complete external sorting
cpd配准存在的问题
Talking about DOM objects in depth, use four versions of demoplus to break the history.state header (more)
[HMS core] [FAQ] [account kit] typical problem set 1
How can VR panoramic display attract users' attention in a new way of online promotion?
Detailed explanation of inheritance
Infrared remote control of FPGA
Instruction arrangement problem
Hcip OSPF interface network type experiment report
数据湖(十八):Flink与Iceberg整合SQL API操作
随机推荐
MySQL series article 4: execution plan
sqlmap的打开方式以代码的形式打开不是以图像形式打开
Unity3d GameObject component
MySQL join和索引
Critical path problem
Misc进阶
MongoDB-查询语句中>、>=、<、<=、=、!=、in、not in用法介绍
Mongodb query statement >, & gt;=、& lt;、& lt;=、=、!=、 In, not in usage introduction
离线安装vscode
PostgreSQL database is deployed on Linux server. Ms level is queried locally. Pgadmin installed on windows is used to query super slow for about 20s. Is it a network problem or a database configuratio
Unity旋转测试
Guys, when Flink SQL job submits a job to yarn, it reports an SQL error that cannot be executed. If it is executed locally, it does not report an error. The server
【云原生】Docker部署数据库的持久化
深度学习实现交叉验证(图像、信号处理)
中国企业管理软件走向全球化国际化的路径探讨
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
STL map
家庭琐事问题
奇瑞星途产品规划曝光,2.0t涡轮增压发动机,年底推出
基于 Flink CDC 实现海量数据的实时同步和转换