string类push_back()方法比用+号空间耗费少,在力扣剑指 Offer 05. 替换空格题目中下面两种方法分别消耗空间6MB,11MB
class Solution { public: string replaceSpace(string s) { string s1=""; for(char e:s){ if(e==' '){ s1.push_back('%'); s1.push_back('2'); s1.push_back('0'); } else s1.push_back(e); } return s1; } };
class Solution { public: string replaceSpace(string s) { string s1=""; for(char e:s){ if(e==' '){ s1=s1+"%20"; } else s1=s1+e; } return s1; } };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)