Sword finger offer interview questions 05. Replace spaces

topic

https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/

Ideas

Traverse each letter of the string into an array and then merge the array into a String

Code

   public String replaceSpace2(String s) {
        if(s==null){
            return s;
        }
        String[] ss = new String[s.length()];
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)==' '){
                ss[i]="%20";
            }else {
                ss[i] = s.charAt(i)+"";
            }
        }
        return String.join("",ss);
    }
Published 33 original articles · praised 37 · 110,000 views

Guess you like

Origin blog.csdn.net/hagle_wang/article/details/104881324