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. 调停者(Mediator)模式

    调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从而使它们可以较松散地耦合.当这些对象中的某些对象之间的相互作用发生改变时,不会立即影响到其他的一些 ...

  2. 文本主题模型之LDA(一) LDA基础

    文本主题模型之LDA(一) LDA基础 文本主题模型之LDA(二) LDA求解之Gibbs采样算法 文本主题模型之LDA(三) LDA求解之变分推断EM算法(TODO) 在前面我们讲到了基于矩阵分解的 ...

  3. XSS研究2-来自内部的XSS攻击的防范

    引入: 前面我们分2篇文章分别探讨了来自外部的XSS攻击和来自内部的XSS攻击,现在我们来专门探讨如何防范来自内部的XSS攻击.   实践:  http://www.cnblogs.com/crazy ...

  4. Android中SimpleAdapter的使用—自定义列表

    本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试 首先我们新建一个layout列表布局文件,具体布局可以自己设定. 下面贴上我的自定义布局文件代码 < ...

  5. Swift 入门之简单语法(二)

    可选项判断 由于可选项的内容可能为 nil,而一旦为 nil 则不允许参与计算 因此使用中,经常需要判断可选项的内容是否为 nil 单个可选项判断 let url = NSURL(string: &q ...

  6. (数字IC)低功耗设计入门(五)——RTL级低功耗设计(续)

    二.RTL级低功耗设计(续) 前面一篇博文我记录了操作数隔离等低功耗设计,这里就主要介绍一下使用门控时钟进行低功耗设计. (4)门控时钟 门控时钟在我的第一篇博客中有简单的描述,这里就进行比较详细的描 ...

  7. Junit4测试Spring

    使用Junit4.4测试 在类上的配置Annotation  @RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境  @ContextCon ...

  8. Ionic进行PC端Web开发时通过脚本压缩提高第一次加载效率

    1. 问题 1.1. 问题上下文描述: 基于Ionic进行PC端的Web应用开发: 使用Tomcat作为最终服务发布容器. 1.2. 问题描述: 编译后main.js的大小为4-6MByte.(集成第 ...

  9. ECS_8080端口连接拒绝问题排查

    原文链接 用户ECS网络设置 上图是用户ECS的网络示意图: ecs处于vpc网络下 ecs加入了一个安全组,该安全组出入方向均开放8080端口 ecs有两个网卡,一个私网主网卡(有虚线的网卡),一个 ...

  10. <bits/stdc++.h>头文件介绍(包含源代码)

    注:转自http://blog.csdn.net/charles_dong2/article/details/56909347,同为本人写的,有部分修改. 之前在一个小OJ上刷题时发现有人是这么写的: ...