解答

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. C# 索引器的使用

    索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...

  2. Codeforces Round #411 B. 3-palindrome

    B. 3-palindrome time limit per test 1 second memory limit per test 256 megabytes   In the beginning ...

  3. C# 修改GroupBox的边框颜色和字体颜色

    改变GroupBox边框和的颜色 private void groupBox_BasicInformation_Paint(object sender, PaintEventArgs e) { e.G ...

  4. 获取当前时间CTime

    std::string getcurtime(){ USES_CONVERSION; CTime z_CurTime; CString z_TimeStr; z_CurTime = CTime::Ge ...

  5. python读写不同编码txt文件

        以后整理规范 import os import codecs filenames=os.listdir(os.getcwd()) out=file("name.txt",& ...

  6. Hyper-V复制

    Hyper-V复制: 默认HV01上的所有虚机都被复制到HV02的 Hyper-V Replica 目录下.虚机启用复制后,当需要启用虚机副本时,要先在HV01上把原虚机关机,然后在HV02上选择故障 ...

  7. fork: retry: Resource temporarily unavailable

    用户A打开文件描述符太多,超过了该用户的限制 修改用户可以打开的文件描述符数量 1.首先,用另一个用户B登录,修改/etc/security/limit.conf * soft nofile 6553 ...

  8. QT的键值对应关系 看完开发节省时间 哈哈

    http://blog.csdn.net/wangjieest/article/details/8283656

  9. 高质量C++C编程指南笔记 标签: c++笔记 2015-11-22 20:59 179人阅读 评论(0) 收藏

    1.  在多重循环中,如果有可能,应当将最长的循环放在最内层,最短的循环放在最外层,以减少 CPU 跨切循环层的次数. 2.  如果循环体内存在逻辑判断,并且循环次数很大,宜将逻辑判断移到循环体的外面 ...

  10. July 15th 2017 Week 28th Saturday

    If I can't hear your heartbeat, you are too far away. 如果我听不见你的心跳,那是因为你离我太远了. Only when the two tight ...