剑指Offer面试题4:替换字符串中的空格

题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入"We are happy.",则输出"We%20are%20happy."

解题方案:
方案一:从前往后替换空格。每找到一个空格将剩余字符都向后移动两位,并用"%20"替换空格,由于有的字符会移动不止一次,所以时间复杂度是o(n2)(假设字符串的长度是n,对每个空格字符,需要移动后面O(n)个字符,因此对含有O(n)个空格字符的字符串而言总的时间效率是O(n2)。),要求字符数组的长度要足够长。
方案二:从后往前替换空格。先遍历字符串,计算出字符串的长度和所含空格的数目,则可以计算出新的字符串的长度是原来字符串的长度+空格数目*2,然后从后往前遍历原来字符串,遇到空格则用"%20"替换,直到没有空格为止。这样每个字符移动的次数只有一次,故时间复杂度是o(n),同样要求字符数组的总长度要足够容纳新的字符串。

故选择方案二。

代码实现如下:

#include <iostream>

using namespace std;

//length是字符数组的总长度
void ReplaceBlank(char string[], int length)
{
    if (string == NULL || length <= 0)
    {
        return;
    }
    int originalLength = 0;
    int numberOfBlank = 0;
    int i = 0;
    while (string[i] != '\0')
    {
        ++originalLength;
        if (string[i] == ' ')
        {
            ++numberOfBlank;
        }
        ++i;
    }
    //newLength不包括空格
    int newLength = originalLength + numberOfBlank * 2;
    if (newLength > length)
    {
        return;
    }
    int indexOfOriginal = originalLength;
    int indexOfNew = newLength;
    while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
    {
        if (string[indexOfOriginal] == ' ')
        {
            string[indexOfNew--] = '0';
            string[indexOfNew--] = '2';
            string[indexOfNew--] = '%';
        }
        else
        {
            string[indexOfNew--] = string[indexOfOriginal];
        }
        --indexOfOriginal;
    }
}

//测试
void Test(char* testName, char string[], int length, char expected[])
{
    if (testName != NULL)
    {
        cout << testName << " ";
    }
    ReplaceBlank(string, length);
    if (string == NULL && expected == NULL)
    {
        cout << "Passed." << endl;
    }
    else if (string == NULL && expected != NULL)
    {
        cout << "Failed." << endl;
    }
    else if (strcmp(string, expected) == 0)
    {
        cout << "Passed." << endl;
    }
    else
    {
        cout << "Failed." << endl;
    }
}

//字符串中有一个空格
void Test1()
{
    const int length = 100;
    char string[length] = "hello world";
    Test("Test1", string, length, "hello%20world");
}

//字符串头部有一个空格
void Test2()
{
    const int length = 100;
    char string[length] = " helloworld";
    Test("Test2", string, length, "%20helloworld");
}

//字符串尾部有一个空格
void Test3()
{
    const int length = 100;
    char string[length] = "helloworld ";
    Test("Test3", string, length, "helloworld%20");
}

//字符串中有连续空格
void Test4()
{
    const int length = 100;
    char string[length] = "hello  world";
    Test("Test4", string, length, "hello%20%20world");
}

//字符串中没有空格
void Test5()
{
    const int length = 100;
    char string[length] = "helloworld";
    Test("Test5", string, length, "helloworld");
}

//输入的字符串为NULL
void Test6()
{
    const int length = 100;
    Test("Test6", NULL, length, NULL);
}

//输入的字符串是个空串
void Test7()
{
    const int length = 100;
    char string[length] = "";
    Test("Test7", string, length, "");
}

//字符串中只有一个空格
void Test8()
{
    const int length = 100;
    char string[length] = " ";
    Test("Test8", string, length, "%20");
}

//字符串中只有多个空格
void Test9()
{
    const int length = 100;
    char string[length] = "   ";
    Test("Test9", string, length, "%20%20%20");
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    Test8();
    Test9();

    return 0;
}

测试结果:
Test1 Passed.
Test2 Passed.
Test3 Passed.
Test4 Passed.
Test5 Passed.
Test6 Passed.
Test7 Passed.
Test8 Passed.
Test9 Passed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值