题解 | #替换空格#
替换空格
https://www.nowcoder.com/practice/0e26e5551f2b489b9f58bc83aa4b6c68
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串
*/
string replaceSpace(string s) {
// write code here
for(int i=0;i<s.size();++i) //原地操作,i < s.size()必须写在for语句中,动态变化
{
if(s[i]==' ')
{
s[i]='%';
s.insert(i+1, "20");
}
}
return s;
}
};
//法二
/*string replaceSpace(string s) {
// write code here
string res; //额外的辅助空间,空间复杂度O(n)
for (char a : s){
if (a == ' '){
res += "%20";
}else{
res += a;
}
}
return res;
}
*/