替换空格

题目描述

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

class Solution {
public:
	void replaceSpace(char *str,int length) {
        int count = 0;
        for(int i=0;i<length;i++){
            if(str[i]==' ')
                count++;
        }
        for(int i=length-1;i>=0;i--){
            if(str[i]!=' '){
                str[i+2*count]=str[i];
            }else{
                count--;
                str[i+2*count]='%';
                str[i+2*count+1]='2';
                str[i+2*count+2]='0';
            }
        }
	}
};


思路:从前向后记录‘ ’数目,从后向前替换‘ ’。 重点:从后向前替换的时候的技巧 例如:“we are lucky”

0 1 2 3 4 5 6 7 8 9 10 11
w e a r e l u c k y
可以得知count=2;//空格的个数。 所以在替换的时候7~11的字母要向后移动count×2个位置,3~5字母要向后移动(count-1)×2个位置。 所以得到 :

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
w e   a r e   l u c  k y 
w e   a r a r e u c  k l u c k y
在替换的时候直接在空格处写入%20了

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
w e   a r e   l u c  k y 
w e % 2 0 a r e % 2  0 l u c k y
class Solution {
public:
	void replaceSpace(char *str,int length) {
        if(str==NULL||length<0){
            return ;
        }
        int i=0;
        int oldnumber=0;
        int count=0;
        while(str[i]!='\0'){
            oldnumber++;
            if(str[i]==' '){
                count++;
            }
            i++;
        }
        int newlength=oldnumber+count*2;

        int pOldlength=oldnumber;
        int pNewlength=newlength;
        while(pOldlength>=0&&pNewlength>pOldlength){
            if(str[pOldlength]==' '){
                str[pNewlength--]='0';
                str[pNewlength--]='2';
                str[pNewlength--]='%';
            }else{
                str[pNewlength--]=str[pOldlength];
            }
            pOldlength--;
        }
	}
};

 

posted @ 2019-03-26 17:40  strawqqhat  阅读(100)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }