直接遍历一遍字符串,使用一个StringBuilder res(使用这个效率高一点)来存储答案,当遇到空格的时候,直接res.append("%20"),其余的直接append就行了
class Solution {
    public String replaceSpace(String s) {
        StringBuilder res = new StringBuilder();
        for(char c : s.toCharArray()){
            if(c == ‘ ‘) res.append("%20");
            else res.append(c);
        }
        return res.toString();
    }
}
原文:https://www.cnblogs.com/Lngstart/p/14624632.html