underwindfall / Algorithme

练习总结算法的地方
https://qifanyang.com/resume
1 stars 0 forks source link

LCOF05 #346

Closed underwindfall closed 2 years ago

underwindfall commented 2 years ago
 // time O(n)
    // space O(1)
    public String replaceSpace(String s) {
        String c = "%20";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                sb.append(c);
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }