zoukankan      html  css  js  c++  java
  • 《剑指offer》第五十八题(左旋转字符串)

    // 面试题58(二):左旋转字符串
    // 题目:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。
    // 请定义一个函数实现字符串左旋转操作的功能。比如输入字符串"abcdefg"和数
    // 字2,该函数将返回左旋转2位得到的结果"cdefgab"。
    
    #include <iostream>
    #include <string>
    
    void Reverse(char *pBegin, char *pEnd);
    
    char* LeftRotateString(char* pStr, int n)
    {
        if (pStr != nullptr)//防止为空,这很重要
        {
            int nLength = static_cast<int>(strlen(pStr));
            if (nLength > 0 && n > 0 && n < nLength)
            {
                char* pFirstStart = pStr;//设置两段,前n个为一段
                char* pFirstEnd = pStr + n - 1;
                char* pSecondStart = pStr + n;//后面为一段
                char* pSecondEnd = pStr + nLength - 1;
    
                // 翻转字符串的前面n个字符
                Reverse(pFirstStart, pFirstEnd);
                // 翻转字符串的后面部分
                Reverse(pSecondStart, pSecondEnd);
                // 翻转整个字符串
                Reverse(pFirstStart, pSecondEnd);
            }
        }
    
        return pStr;
    }
    
    void Reverse(char *pBegin, char *pEnd)//交换子函数
    {
        if (pBegin == nullptr || pEnd == nullptr)
            return;
    
        while (pBegin < pEnd)
        {
            char temp = *pBegin;
            *pBegin = *pEnd;
            *pEnd = temp;
    
            pBegin++, pEnd--;
        }
    }
    // ====================测试代码====================
    void Test(const char* testName, char* input, int num, const char* expectedResult)
    {
        if (testName != nullptr)
            printf("%s begins: ", testName);
    
        char* result = LeftRotateString(input, num);
    
        if ((input == nullptr && expectedResult == nullptr)
            || (input != nullptr && strcmp(result, expectedResult) == 0))
            printf("Passed.
    
    ");
        else
            printf("Failed.
    
    ");
    }
    
    // 功能测试
    void Test1()
    {
        char input[] = "abcdefg";
        char expected[] = "cdefgab";
    
        Test("Test1", input, 2, expected);
    }
    
    // 边界值测试
    void Test2()
    {
        char input[] = "abcdefg";
        char expected[] = "bcdefga";
    
        Test("Test2", input, 1, expected);
    }
    
    // 边界值测试
    void Test3()
    {
        char input[] = "abcdefg";
        char expected[] = "gabcdef";
    
        Test("Test3", input, 6, expected);
    }
    
    // 鲁棒性测试
    void Test4()
    {
        Test("Test4", nullptr, 6, nullptr);
    }
    
    // 鲁棒性测试
    void Test5()
    {
        char input[] = "abcdefg";
        char expected[] = "abcdefg";
    
        Test("Test5", input, 0, expected);
    }
    
    // 鲁棒性测试
    void Test6()
    {
        char input[] = "abcdefg";
        char expected[] = "abcdefg";
    
        Test("Test6", input, 7, expected);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        system("pause");
        return 0;
    }
  • 相关阅读:
    生成XML文件
    webService的发布与调用
    does not contain bitcode ShardSDK等三方库
    IOS在Document目录下创建文件夹、保存、读取、以及删除文件
    判断IOS安装后是否是第一次启动
    OC中使用单例模式
    两个时间(日期)段交集判断方法
    $(document).ready vs. $(window).load
    基于vant上传图片添加水印
    常用的正则校验
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10544865.html
Copyright © 2011-2022 走看看