zoukankan      html  css  js  c++  java
  • [剑指offer] 44. 翻转单词顺序列

    题目描述

    牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
    class Solution
    {
      public:
        string ReverseSentence(string str)
        {
            if (str == "")
                return "";
            vector<string> strs;
            string temp = "";
            string res = "";
            for (int i = 0; i < str.length(); i++)
            {
                if (str[i] != ' ')
                    temp += str[i];
                else
                {
                    strs.push_back(temp);
                    temp = "";
                }
            }
            strs.push_back(temp);
    
            for (int i = strs.size() - 1; i >= 0; i--)
            {
                res += strs[i];
                if (i != 0)
                    res += " ";
            }
            return res;
        }
    };
  • 相关阅读:
    Node.js REPL(交互式解释器)
    Node.js NPM 使用介绍
    Node.js 创建第一个应用
    Node.js 安装配置
    Node.js 教程
    Go语言-通道类型
    golang 线程与通道
    Go 语言 goto 语句
    Go 语言指向指针的指针
    session效率
  • 原文地址:https://www.cnblogs.com/ruoh3kou/p/10156480.html
Copyright © 2011-2022 走看看