557. Reverse Words in a String III

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.

思路:可以利用栈,注意边界的处理。

class Solution {
public:
string reverseWords(string s) {
int length = s.length();
string result(length, '\0');
stack<char> stk;
int m = ;
for (int i = ;i<length;i++)
{
if (s[i] != ' ')
{
stk.push(s[i]);
}
if (s[i] == ' ' || i==length-)
{
while (!stk.empty())
{
char e = stk.top();
result[m] = e;
stk.pop();
++m;
}
if(i!=length-)
{
result[m] = ' ';
++m;
}
}
}
return result;
}
};

53. leetcode557. 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

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

  4. ledecode Reverse Words in a String III

    557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...

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

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

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

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

  8. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

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

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

随机推荐

  1. ionic 中关于日期的转换格式

    //在HTML页面上{{ 2015-12-07T15:59:59.000Z | date }} //结果:December 7, 2015 {{ 2015-12-07T15:59:59.000Z | ...

  2. oracle表空间自增长

    方式一:通过修改oracle database control 修改 第一步,点击开始--所有程序--Oracle - OraDb11g_home1--Database Control 第二步,通过g ...

  3. ecshop商品页增加编辑器fckeditor

    最近在做ecshop的项目,需要在商品单页中增加一项FCKEditor的文本编辑器,但在ecshop的论坛和百度里搜出的方法,试了好几个都没有用,终于找到一个可以正确使用的,和大家分享. ecshop ...

  4. 手动整合实现SSH项目开发01

    内容简介:本文主要介绍SSH项目开发的配置以及简单登录功能的实现. 1. 新建一个Dynamic Web Project. 2.导入需要 的Jar包,此项目是Struts.Hibernate.Spri ...

  5. 开源框架GreenDao的操作

    1.为什么需要GreenDao?Google原生API不方便 @1手动组拼SQL语句 @2需要自己写操作数据库代码 @3不能把数据库中的数据映射成对象 @4没有实现关联查询 2.GreenDao是什么 ...

  6. 【node】记录项目的开始与完成——pipeline_kafka流式数据库管理项目

    前言: 我始终坚信的一点是,学习的效果80%来自总结,甚至全部都是.总结的好处就是让你能翻出你的过往,指出其中的不足,看到未来的改进方法,好的总结更能让知识产生飞跃,所以在工作之余,部署项目之际,总结 ...

  7. mysql 列类型以及属性特点

    整形列: 一个字节有8个位,例如:int 类型的列存入数字1,00000000 00000000 00000000 00000001它就在最低位置上存入一个1,由此可见是极大的浪费资源,所以在建立列类 ...

  8. Kafka 源代码分析之LogManager

    这里分析kafka 0.8.2的LogManager logmanager是kafka用来管理log文件的子系统.源代码文件在log目录下. 这里会逐步分析logmanager的源代码.首先看clas ...

  9. Linux之正则表达式

    正则表达式与通配符的区别: 最常应用正则表达式的命令是grep(egrep),sed,awk. 正则表达式和通配符有本质区别,正则表达式用来找:[文件]内容,文本,字符串.一般只有三剑客支持.通配符用 ...

  10. 给xcode项目修改名字

    在xcode项目开发中,经常会遇到需要修改项目名字的问题, 但是xcode本身修改项目名字比较麻烦,有时候修改的不完全,有时候修改了项目无法打开,无奈只能建一个新项目.这里提供一种修改xcode项目名 ...