zoukankan      html  css  js  c++  java
  • 《剑指offer》第五十八题I:翻转单词顺序

    // 面试题58(一):翻转单词顺序
    // 题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
    // 为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. ",
    // 则输出"student. a am I"。
    
    #include <cstdio>
    #include "StringUtil.h"
    #include <string>
    
    char* ReverseSentence(char* pData)
    {
        if (pData == nullptr)
            return nullptr;
    
        char* pBegin = pData;
        char* pEnd = pData;
    
        //找字符串结尾
        while (*pEnd != '')
            ++pEnd;
        --pEnd;
    
        Reverse(pBegin, pEnd);  //第一次反转
    
        pBegin = pEnd = pData;
        while (*pBegin != '')
        {
            if (*pBegin == ' ')  //此时已经完成一个单词的反转, pBegin == pEnd
            {
                ++pBegin;
                ++pEnd;
            }
            else if (*pEnd == ' ' || *pEnd == '')  //找到单词末尾
            {
                Reverse(pBegin, --pEnd);  //反转单词, 注意不是引用, 因此pBegin和pEnd值不会改变
                pBegin = ++pEnd;
            }
            else
                ++pEnd;
        }
        return pData;
    }
    // ====================测试代码====================
    void Test(const char* testName, char* input, const char* expectedResult)
    {
        if (testName != nullptr)
            printf("%s begins: ", testName);
    
        ReverseSentence(input);
    
        if ((input == nullptr && expectedResult == nullptr)
            || (input != nullptr && strcmp(input, expectedResult) == 0))
            printf("Passed.
    
    ");
        else
            printf("Failed.
    
    ");
    }
    
    // 功能测试,句子中有多个单词
    void Test1()
    {
        char input[] = "I am a student.";
        char expected[] = "student. a am I";
    
        Test("Test1", input, expected);
    }
    
    // 功能测试,句子中只有一个单词
    void Test2()
    {
        char input[] = "Wonderful";
        char expected[] = "Wonderful";
    
        Test("Test2", input, expected);
    }
    
    // 鲁棒性测试
    void Test3()
    {
        Test("Test3", nullptr, nullptr);
    }
    
    // 边界值测试,测试空字符串
    void Test4()
    {
        char input[] = "";
        char expected[] = "";
        Test("Test4", input, expected);
    }
    
    // 边界值测试,字符串中只有空格
    void Test5()
    {
        char input[] = "   ";
        char expected[] = "   ";
        Test("Test5", input, expected);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
    
        return 0;
    }
    测试代码
    #include "StringUtil.h"
    
    void Reverse(char *pBegin, char *pEnd)
    {
        if(pBegin == nullptr || pEnd == nullptr)
            return;
    
        while(pBegin < pEnd)
        {
            char temp = *pBegin;
            *pBegin = *pEnd;
            *pEnd = temp;
    
            pBegin ++, pEnd --;
        }
    }
    StringUtil.cpp

    分析:不是引用传递不会改变值。

    class Solution {
    public:
        string ReverseSentence(string str) {
            
            int begin = 0;
            int end = str.length() - 1;
            
            Reverse(str, begin, end);
            
            begin = end = 0;
            while (str[begin] != '')
            {
                if (str[begin] == ' ')
                {
                    ++begin;
                    ++end;
                }
                else if (str[end] == ' ' || str[end] == '')
                {
                    Reverse(str, begin, --end);
                    begin = ++end;
                }
                else
                    ++end;
            }
            return str;
        }
        
        void Reverse(string &str, int begin, int end)
        {
            while (begin < end)
            {
                char temp = str[begin];
                str[begin] = str[end];
                str[end] = temp;
                
                ++begin;
                --end;
            }
        }
    };
    牛客网提交代码
  • 相关阅读:
    从零开始学 Web 之 DOM(七)事件冒泡
    从零开始学 Web 之 DOM(六)为元素绑定与解绑事件
    从零开始学 Web 之 DOM(五)元素的创建
    从零开始学 Web 之 DOM(四)节点
    从零开始学 Web 之 DOM(三)innerText与innerHTML、自定义属性
    从零开始学 Web 之 DOM(二)对样式的操作,获取元素的方式
    从零开始学 Web 之 DOM(一)DOM的概念,对标签操作
    @RequestParam和@GetMapping
    组件(一)
    Vue(一)
  • 原文地址:https://www.cnblogs.com/ZSY-blog/p/12661883.html
Copyright © 2011-2022 走看看