class Solution { public: void replaceSpace(char *str,int length) { int count = 0; //计算空格的数量 for(int i = 0; i < length; i++){ if(str[i] == ‘ ‘) count++; } if(count == 0 || length <= 0) return; //no space for(int i = length - 1; i >= 0; i--){ if(str[i] != ‘ ‘) { str[i + 2*count] = str[i]; } else{ count--; //每次替换都要减少count str[i + 2*count] = ‘%‘; str[i + 2*count + 1] = ‘2‘; str[i + 2*count + 2] = ‘0‘; } } } };
原文:https://www.cnblogs.com/BillowJ/p/12676713.html