-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathreplaceSpace.h
More file actions
76 lines (70 loc) · 2.41 KB
/
replaceSpace.h
File metadata and controls
76 lines (70 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// replaceSpace.h
// string
//
// Created by junlongj on 2019/8/3.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef replaceSpace_hpp
#define replaceSpace_hpp
#include <stdio.h>
/*
剑指Offer(二):替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
namespace codinginterviews{
/*
思路:将空格替换为%20,那么字符长度肯定会变长,为了不让下标变化,我们从后往前遍历。只要遇到空格,就将空格后面的字符整体x向后移动2个位置,这样就可以插入%20了》
*/
#pragma mark - 暴力法
void replaceSpace(char *str,int length) {
int i=length-1;
while (i>=0) {
if (str[i] == ' ') {
//进行替换
int j = length-1;
for (; j>=i+1; j--) {
str[j+2]=str[j];
}
length+=2;
str[j++] = '%';
str[j++] = '2';
str[j++] = '0';
i--;
}else{
i--;
}
}
}
#pragma mark - 优解
void replaceSpace2(char *str,int length){
/*
上面的思路,每次遇到空格都会重复的移动大量数据。所以,我们可以提前计算好空格的个数,这样新的字符串的长度就知道了,那么一次移动就ok。
*/
int count = 0;
for (int i=0; i<length; i++) {
if (str[i] == ' ') {
count++;
}
}
int newlen = length+count*2;
for (int i=length-1,j=newlen-1; i>=0&&j>=0;) {//i指向老字符串的末尾,j指向新字符串的末尾
if (str[i] == ' ') {
str[j--] = '0';
str[j--] = '2';
str[j--] = '%';
i--;
}else{
str[j--] = str[i--];
}
}
}
void test_replaceSpace(){
char a[100] = "We Are Happy";
replaceSpace2(a, 13);
std::cout << "test_replaceSpace starting ......." << std::endl;
std::cout << a << std::endl;
}
}
#endif /* replaceSpace_hpp */