解答

class Solution {
public:
    string reverseWords(string s) {
        string temp,result;
        while(1){
            if(s.find(' ')!=string::npos){
                temp=s.substr(0,s.find(' '));
                reverse(temp.begin(),temp.end());
                result += temp+" ";
                s=s.substr(s.find(' ')+1);
            }
            else{
                temp=s;
                reverse(temp.begin(),temp.end());
                result += temp;
                break;
            }
        }
        return result;
    }
};

笔记

  1. 循环的问题,我之前是用s.empty()判断来作为判断条件的,结果会进入死循环。
  2. 找不到空格说明已经只剩最后一个单词了,需要单独处理,
  3. 找到空格时创建新串要加上空格
result += temp + " ";

补充

  1. 其实可以用s.empty()来作为循环的条件,处理方式为:
条件:      1 改为 !s.empty()
else中:     break 改为 s.clear()

557. Reverse Words in a String III (5月25日)的更多相关文章

  1. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  2. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

  3. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  4. 557. Reverse Words in a String III【easy】

    557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...

  5. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  6. Week4 - 500.Keyboard Row & 557.Reverse Words in a String III

    500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...

  7. 557. Reverse Words in a String III 翻转句子中的每一个单词

    [抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...

  8. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  9. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  10. LeetCode 557 Reverse Words in a String III 解题报告

    题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...

随机推荐

  1. oracle数据库基本命令

    数据库字符集: SQL> select * from nls_database_parameters where parameter='NLS_CHARACTERSET'; PARAMETER ...

  2. 基础架构之Maven私有库

    Maven对于Java开发来说肯定不会陌生,由于各种问题,公司常常需要搭建自己的私有Maven仓库. (一)  环境要求 Centos 7.5.1804 Docker 18.06.1-ce sonat ...

  3. 使用SlidingPaneLayout 实现仿微信的滑动返回

    上周,公司的项目改版要求加上一个右滑返回上一个界面,于是就在网上找了一些开源库打算实现.但是在使用的时候遇见了许多的问题.试了两天用过 https://github.com/ikew0ng/Swipe ...

  4. 题目一:使用Java实现二维数组中的查找

    考点:数组       题目:二维数组中的查找   描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判 ...

  5. pymsql模块使用

    数据库连接客户端 链接:https://pan.baidu.com/s/1pM0h4SV 密码:614v  sql指令基本用法:

  6. Linux->Ubuntu配置tomcat开机自动启动

    Ubuntu配置tomcat开机自动启动 我们有时候会有这样一个需求: 在开机的时候就启动一个服务,比如tomcat. 我们可以这样做: 将tomcat目录下/bin中的catalina.sh拷贝到/ ...

  7. shell单引号双引号详解

    linux shell中的单引号与双引号的区别(看完就不会有引号的疑问了) " "(双引号)与 ' '(单引号)的区别    你在shell prompt(shell 提示)后面敲 ...

  8. [原]Linux 命令行浏览器

    真是没有做不到只有想不到! Linux下竟然有命令行式的浏览器:W3m SPC向下翻页 b向上翻页 J 向下滚动一行 K 向上滚动一行 > 右移一屏 < 左移一屏 TAB 转到下个超链接 ...

  9. 记一次挖掘115网盘反射型xss,08xss的储存型xss

    记一次对115分站简单绕过过滤继续实现xss,08xss平台也中枪!! 115反射型xss url:http://115.qiye.115.com/disk/?ac=select_public_fil ...

  10. 十.mysqld_multi stop无效问题

    今天在尝试运行mysqld_report stop的时候,发现无法停止mysql,日志中的错误如下 Stopping MySQL servers mysqladmin: [Warning] Using ...