LintCode-剑指Offer-空格替换

来源:互联网 发布:车载刮胡刀淘宝网 编辑:程序博客网 时间:2024/05/21 02:21
class Solution {
public:
    /**
     * @param string: An array of Char
     * @param length: The true length of the string
     * @return: The true length of new string
     */
    int replaceBlank(char string[], int length) {
        // Write your code here
        char strmy[1000];
        int tmp=0;
        int i;
        for(i=0;i<length;i++)
        {
            strmy[i]=string[i];
        }
        for(i=0;i<length;i++){
            if(strmy[i]==' '){
                string[i+tmp*2]='%';
                string[i+tmp*2+1]='2';
                string[i+tmp*2+2]='0';
                tmp++;
            }else
                string[i+tmp*2]=strmy[i];
        }
        return i+tmp*2;
        
    }
};  
0 0
原创粉丝点击