05. 替換空格

2020-06-22

1.題目描述

請實現一個函數,把字符串 s 中的每個空格替換成"%20"。

2.題解

1.res+="%20";
2.res.push_back('%');
  res.push_back('2');
  res.push_back('0');
class Solution {
public:
    string replaceSpace(string s) {
        string res="";
        int l=s.length();
        for (int i=0;i<l;i++){
            if (s[i]==' ') res+="%20";
            else res+=s[i];
        }
        return res;
    }
};
class Solution {
public:
    string replaceSpace(string s) {
        int len=s.size();
        string res;
        int i=0;
        while (i<len){
            if (s[i]==' '){
                res.push_back('%');
                res.push_back('2');
                res.push_back('0');
            }else{
                res.push_back(s[i]);
            }
            i++;
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章