zoukankan      html  css  js  c++  java
  • (剑指Offer)面试题42:翻转单词顺序

    题目:

    输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。

    例如输入字符串“I am a student.”,则输出"student. a am I".

    思路:

    这是一道常见的面试题,思路分两步:

    1、翻转句子中的所有字符;

    2、翻转句子中每一个单词;

    代码:

    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    void Reverse(char* pBegin,char* pEnd){
        if(pBegin==NULL || pEnd==NULL)
            return;
        char tmp;
        while(pBegin<pEnd){
            tmp=*pBegin;
            *pBegin=*pEnd;
            *pEnd=tmp;
            pBegin++;
            pEnd--;
        }
    }
    
    char* ReverseSentence(char* pData){
        if(pData==NULL)
            return NULL;
    
        char* pBegin=pData;
        /*
        char* pEnd=pData;
        while(*pEnd!='')
            pEnd++;
        pEnd--;
        */
        int len=strlen(pData);
        char* pEnd=pData+len-1;
    
        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 data[]="Hello world!";
        cout << ReverseSentence(data) << endl;
        return 0;
    }

    在线测试OJ:

    http://www.nowcoder.com/books/coding-interviews/3194a4f4cf814f63919d0790578d51f3?rp=2

    AC代码:

    class Solution {
    public:
        string ReverseSentence(string str) {
            int len=str.length();
            if(len<=1)
                return str;
            int pBegin=0;
            int pEnd=len-1;
    
            Reverse(str,pBegin,pEnd);
    
            pBegin=0;
            pEnd=0;
            while(pBegin<=len-1){
                if(str[pBegin]==' '){
                    pBegin++;
                    pEnd++;
                }
                else if(str[pEnd]==' ' || pEnd==len){
                    Reverse(str,pBegin,pEnd-1);
                    pBegin=++pEnd;
                }
                else
                    pEnd++;
            }
    
            return str;
        }
    
        void Reverse(string &str,int pBegin,int pEnd){
            char tmp;
            while(pBegin<pEnd){
                tmp=str[pBegin];
                str[pBegin]=str[pEnd];
                str[pEnd]=tmp;
                pBegin++;
                pEnd--;
            }
        }
    };
  • 相关阅读:
    Codeforces1420E. Battle Lemmings 题解 动态规划
    C++使用partial_sum求前缀和
    HDU6171 Admiral 题解 折半搜索
    HDU3017 Treasure Division 题解 折半搜索
    使用re.split 按标点+空格的一种分割办法
    实现CString的Format功能,支持跨平台
    转载一篇makefile,说的很详细
    Microsoft C++ 异常: std::system_error std::thread
    源码一样,运行结果不一致的解决办法
    记录一次阿里的电话面试
  • 原文地址:https://www.cnblogs.com/AndyJee/p/4683100.html
Copyright © 2011-2022 走看看