标签:提取 esc 接下来 size while number color 字符 ret
要求:时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
class Solution { public: void replaceSpace(char *str,int length) { if(str == NULL || length < 0)return ; for(int i=0;i < length;) { if(str[i] == ‘ ‘) { length = length + 2; int j = length -1; while(j > i + 2) { str[j] = str[j-2]; j--; } str[i] = ‘%‘; str[i+1] = ‘2‘; str[i+2] = ‘0‘; i = j+1; } else i++; } } };
lass Solution { public: void replaceSpace(char *str,int length) { if(str == NULL || length < 0)return; int i = 0; int oldnumber = 0;//记录以前的长度 int replacenumber = 0;//空格的数目 while(str[i] != ‘\0‘) { oldnumber ++; if(str[i] == ‘ ‘) { replacenumber ++; } i++; } int newlength = oldnumber + 2 * replacenumber;//插入后的长度 if(newlength > length)//如果计算后的长度大于总长度就无法插入,说明无空格,不需要插入 { return ; } int pOldlength = oldnumber;//字符串以‘\0‘结尾 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--;//不管是if还是else都要把pOldlength前移 } } };
标签:提取 esc 接下来 size while number color 字符 ret
原文地址:https://www.cnblogs.com/whiteBear/p/12386380.html