解答

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. CSS3及JS简单实现选项卡效果(适配手机端和pc端)

    想要适配手机端和pc端,有几种简单的方法,本人使用的是百分比分配的方法. *{ padding: 0; margin: 0; } body,html{ width: 100%; height: 100 ...

  2. AndroidStudio安装教程

    Android studio安装与配置 1.首先下载Android studio安装包,可以从http://www.android-studio.org/ 2.下载好该安装包之后,点击进行安装,依次出 ...

  3. Incorrect string value: '\xE8\xAF\xAD\xE6\x96\x87' for column 'name' at row 1

    报错的原因就是在执行插入时对Name这个字段被赋予了错误的字符串值:’\xE4\xB8\xAD\xE6\x96\x87’ 实际上就函数里面的变量接收到的值编码格式跟它定义的不一致. 使用navicat ...

  4. git中忽略文件权限或文件拥有者的改变

    在发布项目到线上时,很多时候需要修改文件的权限,如果是使用git版本管理软件来发布的话,那么下次更新线上文件的时候就会提示文件冲突.明明文件没有修改,为什么会冲突呢?原来git把文件权限也算作文件差异 ...

  5. jquery mobile开发中常见的问题(转载)

    1页面缩放显示问题 问题描述: 页面似乎被缩小了,屏幕太宽了. 处理方法: 在head标签内加入: <meta name="viewport" content="w ...

  6. Service Discovery in WCF 4.0 – Part 2 z

    Service Discovery in WCF 4.0 – Part 2 In the previous post I discussed about the basic usage of WCF ...

  7. yii2.0里的redirect跳转方法

    在yii2框架里难免会出现跨控制器跳转,调用方法等,这就用到了redirect了, 带参数的 $control=Yii::app()->runController('site/show/id/2 ...

  8. 如何退出virtualbox scale mode

    进入scale mode之后,可能会退不出来 HOST Key + C. 默认是 右Ctrl + C

  9. 关于cmake输出动态链接库名字的问题

    使用cmake进行项目编译管理时,我们经常使用 add_library(foo SHARED foo.cpp) 这样的话,输出时,如果在win下面会得到foo.dll,linux下面会得到libfoo ...

  10. IM

    一.IM技术概念 IM技术全称Instant Messaging,中文翻译"即时通讯",它是一种使人们能在网上识别在线用户并与他们实时交换消息的技术,是电子邮件发明以来迅速崛起的在 ...