• 周六. 4月 20th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Leetcode-剑指 Offer 05. 替换空格

admin

11月 28, 2021

剑指 Offer 05. 替换空格

请实现一个函数,把字符串 s 中的每个空格替换成”%20″。

遍历检索替换即可。

class Solution {
public:
    string replaceSpace(string s) {
        string ans="";
        for (int i=0;i<s.size();i++)
            if (s[i]==' ') ans+="%20";
            else ans+=s[i];
        return ans;
    }
};

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注