Given a string, you need to reverse the order of characters in each word within a sentence while
still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
思路:

用一个vector<vector<char> >收集所有的字符,空格为单词分割符,将每一个单词翻转后,输出即可。

感觉思路还比较朴素,但是看上去很啰嗦。

string reverseWords(string s)
{
if(s == "")return s;
vector<vector<char> >sentence;
vector<char>word;
int i=;
while(s[i] != '\0')
{
if(s[i]!=' ')
{
word.insert(word.begin(),s[i]);
}
else
{
sentence.push_back(word);
word.clear();
}
i++;
}
sentence.push_back(word);
char res[];//太小 要用100000
int ind =;
for(int i=;i<sentence.size();i++)
{
for(int j=;j<sentence[i].size();j++)
{
res[ind] = sentence[i][j];
ind++;
}
res[ind] = ' ';
ind++;
}
res[ind-] = '\0';
return res;
}

[leetcode-557-Reverse Words in a String III]的更多相关文章

  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 翻转字符串中的单词 III

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

  4. Leetcode - 557. Reverse Words in a String III (C++) stringstream

    1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...

  5. 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 ...

  6. 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 ...

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

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

  8. 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 ...

  9. 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 ...

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

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

随机推荐

  1. .NET面试题系列[18] - 多线程同步(1)

    多线程:线程同步 同步基本概念 多个线程同时访问共享资源时,线程同步用于防止数据损坏或发生无法预知的结果.对于仅仅是读取或者多个线程不可能同时接触到数据的情况,则完全不需要进行同步. 线程同步通常是使 ...

  2. 仿中关村win8频道(win8.zol.com.cn)下的tab效果

    最近觉得中关村win8频道下的那个Tab效果很好看. 一时兴起就自己做了一个.觉得还蛮不错的,特地来给大家分享一下.以下是相关的HTML页面写法: <div class="popula ...

  3. 我对Stub和Mock的理解

    介绍 使用测试驱动开发大半年了,我还是对Stub和Mock的认识比较模糊,没有进行系统整理. 今天查阅了相关资料,觉得写得很不错,所以我试图在博文中对资料进行整理一下,再加上一些自己的观点. 本文是目 ...

  4. xpo-4大类

      Xpo (XPBaseObject.XPLiteObject.XPCustomObject.XPObject) 类名 延后删除 是否乐观锁定 提供OID字段 XPBaseObject 不支持 支持 ...

  5. Node.js~ioredis处理耗时请求时连接数瀑增

    回到目录 关于redis连接数过高的解释 对于node.js开发环境里,使用传统的redis或者使用ioredis都是不错的选择,而在处理大数据请求程中,偶尔出现了连接池( redis服务端的最大可用 ...

  6. YYLabel 自动布局 富文本文字点击事件

    YYLabel显示多行除了需要设置numberOfLines = 0以外,还需要设置preferredMaxLayoutWidth最大的宽度值才可以生效多行效果 YYLabel中的NSMutableA ...

  7. VHDL乘除法及转换

    首先鄙视一下这个不智能的语言 1.要进行乘法与除法,数据类型必须是signed 2.两个16位的数相乘,结果必须是32位的 3.乘以2的n次幂的数可以直接乘,之后截位也比较方便,(其实直接移位就可以) ...

  8. .net实现多重继承问题(virtual)

    C#中是没有类的多重继承这个概念.要使用多重继承必须要通过接口Interface来完成, 一.接口类 interface  getTable{      DataTable Getdatatable( ...

  9. 打开Eclipse弹出“No java virtual machine was found..."的解决方法

    今天准备用Eclipse抓取Android应用崩溃log,打开Eclipse时发现运行不了有以下弹框 A Java Runtime Environment(JRE) or Java Developme ...

  10. Java servlet ajax

    AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下. AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. http://w ...