zoukankan      html  css  js  c++  java
  • 【面试题042】翻转单词顺序VS左旋转字符串

    【面试题042】翻转单词顺序VS左旋转字符串
    题目一:
        输入一个英文句子,反转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。
        例如输入字符串“I am a student.”,则输出“student. a am I”。
    思路一:
        反转字符串的顺序,这样子每个单词的顺序也反转了,然后再次反转每个单词,这样子就做到了题目要求。
    ——关键是实现一个函数以反转字符串中的一段。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
     
    #include <iostream>

    using namespace std;


    void Reverse(char *pBegin, char *pEnd)
    {
        if (pBegin == NULL || pEnd == NULL)
        {
            return ;
        }
        while(pBegin < pEnd)
        {
            char temp = *pBegin;
            *pBegin = *pEnd;
            *pEnd = temp;

            pBegin ++;
            pEnd --;
        }
    }

    char *ReverseSentence(char *pData)
    {
        if(pData == NULL)
            return NULL;

        char *pBegin = pData;

        char *pEnd = pData;
        while(*pEnd != '')
            pEnd ++;
        pEnd--;

        // 翻转整个句子
        Reverse(pBegin, pEnd);

        // 翻转句子中的每个单词
        pBegin = pEnd = pData;
        while(*pBegin != '')
        {
            if(*pBegin == ' ')
            {
                pBegin ++;
                pEnd ++;
            }
            else if(*pEnd == ' ' || *pEnd == '')
            {
                Reverse(pBegin, --pEnd);
                pBegin = ++pEnd;
            }
            else
            {
                pEnd ++;
            }
        }

        return pData;
    }

    int main()
    {
        char str[] = "I am a student.";

        ReverseSentence(str);
        printf("%s ", str);

        return 0;
    }
     
     
    题目二:
        字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的末尾。请定义一个函数实现字符串左旋转操作的功能。
    比如输入字符串“abcdefg”和数字2,该函数将返回左旋转2位得到的结果“cdefgab”。
     
    思路一:
        前两个字符ab为一部分,后面五个字符为一部分cdefg,然后两次反转,即可达到目的。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
     
    #include <iostream>
    #include <string>
    using namespace std;


    void Reverse(char *pBegin, char *pEnd)
    {
        if (pBegin == NULL || pEnd == NULL)
        {
            return ;
        }
        while(pBegin < pEnd)
        {
            char temp = *pBegin;
            *pBegin = *pEnd;
            *pEnd = temp;

            pBegin ++;
            pEnd --;
        }
    }

    char *LeftRotateString(char *pStr, int n)
    {
        if (pStr != NULL)
        {
            int nLength = static_cast<int>(strlen(pStr));
            if (nLength > 0 && n > 0 && n < nLength)
            {
                char *pFirstStart = pStr;
                char *pFirstEnd = pStr + n - 1;
                char *pSecondStart = pStr + n;
                char *pSecondEnd = pStr + nLength - 1;

                Reverse(pFirstStart, pFirstEnd);
                Reverse(pSecondStart, pSecondEnd);
                Reverse(pFirstStart, pSecondEnd);
            }
        }
        return pStr;
    }

    int main()
    {
        char str[] = "abcdefg";
        LeftRotateString(str, 2);
        printf("%s ", str);

        return 0;
    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    shell编程学习笔记(七):Shell中将指定内容输出到文件中
    shell编程学习笔记(六):cat命令的使用
    shell编程学习笔记(五):Shell中脚本的参数
    shell编程学习笔记(四):Shell中转义字符的输出
    shell编程学习笔记(三):Shell中局部变量的使用
    shell编程学习笔记(二):Shell中变量的使用
    shell编程学习笔记(一):编写我的第一段代码
    VMware DHCP Service服务无法启动问题的解决
    Jmeterr修改显示字体大小
    jenkins流水线pipeline脚本实例
  • 原文地址:https://www.cnblogs.com/codemylife/p/3762141.html
Copyright © 2011-2022 走看看