问题:
设计一种方法,将一个字符串中的所有空格替换成 %20
。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。
你的程序还需要返回被替换后的字符串的长度。
样例:
样例 1:
输入:string[] = "Mr John Smith" and length = 13 输出:string[] = "Mr%20John%20Smith" and return 17 解释: 对于字符串 "Mr John Smith",长度为 13。替换空格之后,参数中的字符串需要变为 "Mr%20John%20Smith",并且把新长度 17 作为结果返回。
样例 2:
输入:string[] = "LintCode and Jiuzhang" and length = 21 输出:string[] = "LintCode%20and%20Jiuzhang" and return 25 解释: 对于字符串 "LintCode and Jiuzhang",长度为 21。替换空格之后,参数中的字符串需要变为 "LintCode%20and%20Jiuzhang",并且把新长度 25 作为结果返回。
python:
class Solution:
"""
@param: string: An array of Char
@param: length: The true length of the string
@return: The true length of new string
"""
def replaceBlank(self, string, length):
# write your code here
if string == None:
return None
resultNum = length
for i in range(length):
if string[i] == ' ':
resultNum+=2
strIndex = resultNum - 1
for j in range(length-1,-1,-1):
if(string[j] == ' '):
string[strIndex] = '0'
strIndex -= 1
string[strIndex] = '2'
strIndex -= 1
string[strIndex] = '%'
else:
string[strIndex] = string[j]
strIndex -= 1
return resultNum
C++:
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
int resultNum = length;
for(int i = 0; i < length; i++)
{
if(string[i] == ' ')
{
resultNum+=2;
}
}
int strIndex = resultNum-1;
for(int j = length-1; j >=0; j-- )
{
if(string[j] == ' ')
{
string[strIndex] = '0';
strIndex--;
string[strIndex] = '2';
strIndex--;
string[strIndex] = '%';
}
else
{
string[strIndex] = string[j];
}
strIndex--;
}
return resultNum;
}
};
PS:注意用单引号