Sword offer - replace spaces

1. Question: Please implement a function to replace spaces in a string with "%20" . For example, when the string is We Are Happy., the replaced string is We%20Are%20Happy.

2. Idea: The first thing that comes to mind is str.replaceAll("\\s", "%20"), which can definitely be written like this at work, but if it is an interview, the main test is programming ability, not familiarity the extent of the API;

             One idea is to replace the original string, but it can only be used when the length of the replaced character is the same as the length of the replaced character;

            Another idea is to first calculate the length of the string after the replacement, and then create a new string to store the replaced string. This method is used here;

3. Code:

①: Do not use StringBuilder and StringBuffer

    public String replaceSpace(String s) {
         // 1. Determine how many spaces there are 
        int spaceNum = 0 ;
         for ( int i = 0 ; i < s.length();i++ ){
             if (s.charAt(i) == ' ' ){
                spaceNum++;
            }
        }
        
        if (spaceNum != 0 ){
             // 2. Required length 
            int newLen = s.length() + 2 * spaceNum;
             char [] newChars = new  char [newLen];
             int position = 0 ;

            //3.开始替换
            for(int i = 0  ; i < s.length() ; i++){
                if(s.charAt(i) == ' '){
                   newChars[position++] = '%';
                   newChars[position++] = '2';
                   newChars[position++] = '0'; 
                }else{
                   newChars[position++] = s.charAt(i); 
                }
            }
            
            s = new String(newChars);
        }
      
        return s;
    }

②Use StringBuffer

    public String replaceSpace(StringBuffer str) {
        
        // 1. Determine how many spaces there are 
        int spaceNum = 0 ;
         for ( int i = 0 ; i < str.length();i++ ){
             if (str.charAt(i) == ' ' ){
                spaceNum++;
            }
        }
        
        if (spaceNum != 0 ){
             // 2. Required length 
            int oldLen = str.length();
             int newLen = oldLen + 2 * spaceNum;
            str.setLength(newLen);
            int position = newLen - 1;
            
            // 3. Start replacement, move from back to front, and move only one character each time 
            for ( int i = oldLen - 1 ; i >= 0 ; i-- ){
                 if (str.charAt(i) == ' ' ){
                    str.setCharAt(position-- , '0');
                    str.setCharAt(position-- , '2');
                    str.setCharAt(position-- , '%');
                }else{
                    str.setCharAt (position - , str.charAt (i));
                }
            }
        }
        
        return str.toString();
    }

③Use StringBuilder

 public String replaceSpace(StringBuffer str) {
        if(str == null){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i < str.length();i++){
            if(str.charAt(i) == ' '){
                sb.append('%');
                sb.append('2');
                sb.append('0');
            }else{
                sb.append(str.charAt(i));
            }
        }
        return sb.toString();
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325255456&siteId=291194637