zoukankan      html  css  js  c++  java
  • 《剑指offer》第五题:替换空格

    // 面试题5:替换空格
    // 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
    // 则输出“We%20are%20happy.”。
    
    #include <cstdio>
    #include <cstring>
    
    /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
    void ReplaceBlank(char str[], int length)
    {
        //鲁棒性测试 1.空指针 2.长度小于0
        if (str == nullptr || length <= 0)
            return;
    
        //主要思路:首先进行时间复杂度为O(n)的查找空格,然后从字符串尾部开始后移。
        int number = 0; //空格数
        int true_length = 0; //实际长度
        int i = 0;
        while (str[i] != '') //到结尾
        {
            if (str[i] == ' ') //查找空格
                ++number;
    
            ++true_length;
            ++i;
        }
        //printf("the number of space :%d
    ", number);
    
        //鲁棒性测试 如替换后长度超出限制
        if (true_length + number * 2 > length)
            return;
    
        //true_length 从''开始
        for (int i = true_length; i >= 0; --i) //从尾开始遍历并替换
        {
            if (str[i] == ' ') //找到空格开始替换
            {
                str[i + number * 2 - 0] = '0';
                str[i + number * 2 - 1] = '2';
                str[i + number * 2 - 2] = '%';
                --number; //一开始忘写了, 摸不着头脑
    
                if (number == 0) //提高效率
                    break;
            }
            else
            {
                //非空格挪一下
                str[i + number * 2] = str[i];
            }
        }
    }
    // ====================测试代码====================
    void Test(const char* testName, char str[], int length, const char expected[])
    {
        if (testName != nullptr)
            printf("%s begins: ", testName);
    
        ReplaceBlank(str, length);
    
        if (expected == nullptr && str == nullptr)
            printf("passed.
    ");
        else if (expected == nullptr && str != nullptr)
            printf("failed.
    ");
        else if (strcmp(str, expected) == 0)
            printf("passed.
    ");
        else
            printf("failed.
    ");
    }
    
    // 空格在句子中间
    void Test1()
    {
        const int length = 100;
    
        char str[length] = "hello world";
        Test("Test1", str, length, "hello%20world");
    }
    
    // 空格在句子开头
    void Test2()
    {
        const int length = 100;
    
        char str[length] = " helloworld";
        Test("Test2", str, length, "%20helloworld");
    }
    
    // 空格在句子末尾
    void Test3()
    {
        const int length = 100;
    
        char str[length] = "helloworld ";
        Test("Test3", str, length, "helloworld%20");
    }
    
    // 连续有两个空格
    void Test4()
    {
        const int length = 100;
    
        char str[length] = "hello  world";
        Test("Test4", str, length, "hello%20%20world");
    }
    
    // 传入nullptr
    void Test5()
    {
        Test("Test5", nullptr, 0, nullptr);
    }
    
    // 传入内容为空的字符串
    void Test6()
    {
        const int length = 100;
    
        char str[length] = "";
        Test("Test6", str, length, "");
    }
    
    //传入内容为一个空格的字符串
    void Test7()
    {
        const int length = 100;
    
        char str[length] = " ";
        Test("Test7", str, length, "%20");
    }
    
    // 传入的字符串没有空格
    void Test8()
    {
        const int length = 100;
    
        char str[length] = "helloworld";
        Test("Test8", str, length, "helloworld");
    }
    
    // 传入的字符串全是空格
    void Test9()
    {
        const int length = 100;
    
        char str[length] = "   ";
        Test("Test9", str, length, "%20%20%20");
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
        Test8();
        Test9();
    
        return 0;
    }
    测试代码

    分析:直接建立新旧索引关系,或者单独新建索引(书上代码实现)。

    时间复杂度:O(n) = O(n+n), 空间复杂度:O(1)

    相关题目:基本思路一致,需要额外增加A2数组索引,对比两个数组最后一位的大小,当两个索引都为0时结束。

    时间复杂度:O(n) = O(n), 空间复杂度:O(1)

     

  • 相关阅读:
    [poj_3469]多核CPU
    割点与桥,强连通分量,点双,边双[poj_1236]学校网络
    Iview 启动报错 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
    修改JAVA_HOME失效
    命令模式
    gradle implementation runtimeOnly 和api 区别
    spring boot 整合 RabbitMQ 错误
    关于技术的想法
    eclipse 背景绿豆沙颜色
    抽象工厂模式
  • 原文地址:https://www.cnblogs.com/ZSY-blog/p/12508025.html
Copyright © 2011-2022 走看看