您的位置:首页 > 职场人生

剑指offer-面试题4:替换空格

2016-02-29 20:41 501 查看
题目

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

思路

我们首先想到的就是从前往后扫描,如果空格,就替换为%20,但是这样需要移动空格后的元素。

我们还有一种方法,首先遍历一遍字符串,统计出空格的个数,并可以由此计算出替换之后的字符串的长度。每替换一个空格,长度增加2,因此替换之后的字符串长度等于原来的长度加上2乘以空格的个数。即 替换后的字符串的长度 = 原字符串长度 + 空格数*2。

我们从字符串的后面开始复制和替换。我们首先准备两个指针,p1和p2。指针p1指向原字符串的末尾位置,另一个指针p2指向新字符串的末尾位置。接下来我们移动指针p1,逐个把他指向的字符复制到p2指向的位置,直到碰到一个空格为止。当碰到一个空格时,指针p1向前移动一个位置,在p2之前插入字符串”%20”,同时指针p2向前移动三个位置。

#include <iostream>
using namespace std;

/*length为字符数组string的总容量*/
void ReplaceBlank(char string[], int length)
{
if(string == NULL || length <= 0)
return;

/*originalLength为字符串string的实际长度*/
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(string[i] != '\0')
{
++originalLength;
if(string[i] == ' ')
++numberOfBlank;
++i;
}

/*newLength为把空格替换成‘%20’之后的长度*/
int newLength = originalLength + 2*numberOfBlank;
if(newLength > length)
return;

int indexOfOriginal = originalLength - 1;
int indexOfNew = newLength - 1;
while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
{
if(string[indexOfOriginal] == ' ')
{
string[indexOfNew--] = '0';
string[indexOfNew--] = '2';
string[indexOfNew--] = '%';
}
else
string[indexOfNew--] = string[indexOfOriginal];
indexOfOriginal--;
}
}
void test1()
{
cout << "测试用例1:输入的字符串中包含空格" << endl;
char test1[20] = " wearehappy";
char test2[20] = "wearehappy ";
char test3[20] = "we arehappy";
char test4[20] = "we  arehappy";
ReplaceBlank(test1, 20);
ReplaceBlank(test2, 20);
ReplaceBlank(test3, 20);
ReplaceBlank(test4, 20);

cout << test1 << endl;
cout << test2 << endl;
cout << test3 << endl;
cout << test4 << endl;
}

void test2()
{
cout << "测试用例2:输入的字符串中没有空格" << endl;
char test[20] = "wearehappy";
ReplaceBlank(test, 20);
cout << test << endl;
}

void test3()
{
cout << "测试用例3:特殊输入测试" << endl;
char *test1 = NULL;
char test2[] = "";
char test3[] = " ";
char test4[] = "   ";

ReplaceBlank(test1, 20);
ReplaceBlank(test2, 20);
ReplaceBlank(test3, 20);
ReplaceBlank(test4, 20);

cout << test1 << endl;
cout << test2 << endl;
cout << test3 << endl;
cout << test4 << endl;
}

int main() {
// your code goes here
test1();
test2();
te
befa
st3();
return 0;
}

相关题目:有两个排序的数组A1和A2,内存在A1的末尾有足够多的空余空间容纳A2。请实现一个函数,把A2中的所有数字插入到A1并且所有的数字是排序的。

#include <iostream>
using namespace std;

void Merge(int *A1, int m, int *A2, int n)
{
if(A1 == NULL || A2 == NULL || m <= 0 || n <= 0)
return;
int lengthOfA1 = 0;
int lengthOfA2 = 0;
for(int i = 0; A1[i]; ++i)
lengthOfA1++;
for(int i = 0; A2[i]; ++i)
lengthOfA2++;
int index1 = lengthOfA1 - 1;
int index2 = lengthOfA2 - 1;
int newLength = lengthOfA1 + lengthOfA2 - 1;
while(index2 >=0)
{
int temp;
if(A1[index1] > A2[index2])
{
temp = A1[index1];
--index1;
}
else
{
temp = A2[index2];
--index2;
}
A1[newLength--] = temp;
}
}

int main() {
// your code goes here
int a[20] = {1,3,5,7};
int b[10] = {2,4,5,8};
Merge(a, 20, b, 10);
for(int i = 0; a[i]; ++i)
cout << *(a+i) << " ";
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: