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. Linux 安装 apache2.4.23

    Apache安装要求 必须安装APR.APR-Util.PCRE,gcc-c++等包 Apache httpd: http://mirrors.cnnic.cn/apache//httpd/httpd ...

  2. Hbuilder常用功能汇总

    引用 样式表: mui.min.css Js:mui.min.js 常用功能 获取页面 var webView=plus.webview.currentWebview();//获取当前页 var we ...

  3. MACD指标

    MACD(Moving Average Convergence)平滑异同移动平均线 MACD指标有双移动平均线发展而来,由快速移动平均线减去慢速移动平均线,当MACD从负数转向证书,是买入信号,从正数 ...

  4. 【JAVAWEB学习笔记】05_jQuery基础

    晨读单词: toggle:切换 each:每个(遍历) append:追加(内部追加,将B追加到A的内部结尾处) appendTo:追加(内部追加,将A追加到B的内部结尾处) prepend:追加(内 ...

  5. Two Sum 2015年6月8日

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. 备忘录《一》基于cookie使用拦截器实现客户每次访问自登陆一次

    原创声明:本文为本人原创作品,绝非他处摘取,转载请联系博主 相信大家在各大网站都会遇到,登录时,在登录框出现下次免登陆/一个月免登陆的类似选项,本次博文就是讲解如何实现,在这记录一下,也算是做个备忘录 ...

  7. 使用adb报错;error: unknown host service

    用adb往虚拟机Genymotion上安装apk时报错 报这个错误是因为主机端口5037被占用 接下来就要查看5037被哪个应用程序占用,然后结束该程序,才能使用adb 在cmd输入命令netstat ...

  8. java.util.Properties工具类

    import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import ...

  9. JQuery模拟实现天猫购物车动画效果

    测试程序源代码下载地址:源码 一.功能描述: 1.点击购买按钮,模拟抛物线将物品弹到购物车里: 2.购物车添加物品后,显示+1动画: 效果图如下: 实现如下: 1.导入jquery相关的包: < ...

  10. [原创]CentOS实现智能DNS

    一.       环境: Centos-6.6-x64位操作系统,IP地址:210.38.248.7 二.       安装和配置bind服务: 1.      命令:yum install bind ...