目录

剑指Offer之替换空格

题目描述:

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

解题思路:

时间复杂度: $O(n)$, 空间复杂度: $O(1)$.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
    void replaceSpace(char *str,int length) {
        int count=0;
        for(int i=0; i<length; i++) {
            if(str[i]==' ') 
            {
                count++;
            }
        }
        
        int new_length = count * 2 + length;
        int j = new_length - 1;
        
        for(; length>0; length--)
            if(str[length-1]==' ') 
            {
                str[j--]='0';
                str[j--]='2';
                str[j--]='%';
            } 
            else 
            {
                str[j--]=str[length-1];
            }
    }
};