思路:遍历查找空格进行替换
Python:
class Solution:
def replaceSpace(self, s: str) -> str:
res=[]
for c in s:
if c==' ':
res.append("%20")
else:
res.append(c)
return "".join(res)
C++:
#include<string>
class Solution {
public:
string replaceSpace(string s) {
string res="";
for(int i=0;i<s.length();i++){
if(s[i]==' '){
res+="%20";
//strcat(res,"%20");
}
else{
res+=s[i];
//strcat(res,s[i]);
}
}
return res;
}
};
注意:’/0’和’ ‘输出在屏幕上是一样的,但ascii code不同,’/0’代表字符串的结束
原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/245080.html