-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_4.cpp
60 lines (50 loc) · 1.12 KB
/
1_4.cpp
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
//
// 1_4.cpp
//
//
// Created by Pengyan Qin on 6/14/15.
//
//
#include <iostream>
#include <string>
using namespace std;
// time complexity: O(n)
// space complexity: O(n)
// do not use the space after string, bot optimal
// if don't have the size, we may first calculate the new size, and then change from back to front(in place)
void replace2(string &s, int n){
if(s.length() == 0)
return;
string res;
for(int i = 0; i < n ; ++i){ // auto is the new feature of c++11
if(isgraph(s[i])) // is not space
res += s[i];
else
res += "%20";
}
s = res;
}
// time complexity: O(n)
// space complexity: in place, optimal
void replace1(string &s, int n){
if(s.length() == 0)
return;
int len = s.length();
for(int i = n - 1; i >=0 ; --i){
if(s[i] != ' ')
s[--len] = s[i];
else{
s[--len] = '0';
s[--len] = '2';
s[--len] = '%';
}
}
return;
}
int main(){
string s = "Mr John Smith ";
int n = 13;
replace2(s, n);
cout << s << endl;
return 0;
}