题解 | #替换空格#

替换空格

https://www.nowcoder.com/practice/0e26e5551f2b489b9f58bc83aa4b6c68

class Solution {
public:
    string replaceSpace(string s) {
        // write code here
        int count = 0;
        for (auto str : s){
            if (str == ' ')
                count++;
        }
        int len = s.size();
        s.resize(s.size() + count * 2);
        for (int i = s.size() - 1; i >= 0; i--){
            if (s[len - 1] == ' '){
                s[i] = '0';
                s[i - 1] = '2';
                s[i - 2] = '%';
                i -= 2;
            }
            else 
                s[i] = s[len - 1];
            len--;
        }
        return s;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务