剑指offer 第二题
牛客网 测试算法
解题思路:
这么简单还用解题思路?
#include <string>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串
*/
string replaceSpace(string s) {
// write code here
int len = s.size();
string n1 = "%20";
string str;
for (int i = 0; i < len; i++) {
if (s[i] != ' ') {
cout << s[i] << endl;
str += s[i];
} else {
str += n1;
}
}
return str;
}
};
本地运行:
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main()
{
//vector<vector<int>> matrix( 3, vector <int>(4, 1) );
string s = "We Are Happy";
int len = s.size();
string n1 = "%20";
string str;
for (int i = 0; i < len; i++) {
/*cout << s[i] <<endl;
cout << typeid(s).name() << endl;*/
//s1 = s[i];
if (s[i] != ' ') {
cout << s[i] << endl;
str += s[i];
}
else {
str += n1;
}
}
cout << str << endl;
return 0;
}