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 的缓存 和局部刷新

    最近两天在做项目时,发现ionic的缓存功能非常方便好用,提高了再低端手机特别是android比较低版本上的流畅性!可是,后来发现,整体的缓存整个页面并不是一个一劳永逸的办法,结合局部刷新功能,感觉就 ...

  2. Easyui设置动态表格,动态导出数据实例,附Dome

    最近碰到一个需求,需要提供一个弹出页面选择列表页面需要显示的列,页面确认之后需要修改列表页面显示的表格,导出的数据也需要同步变化. 下面直接上代码 1.设置需要显示的列columus为全局对象,用于子 ...

  3. js传宗接代---继承

    前几天重温了一下js的继承,今天分享给大家: 一,类式继承. 所谓的类式继承就是:第二个类的原型prototype被赋予了第一个类的实例,如subcals.prototype=new supercls ...

  4. python list有关remove的问题

    在python 中进行一次简单的列表循环,当用到remove时出现了一个很有趣的现象, 代码如下: a=range(30) for i in a : if i%4!=0: a.remove(i) 这段 ...

  5. asp.net使用qq邮箱发送邮件

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  6. [leetcode-500-Keyboard Row]

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  7. JavaScript学习笔记(散)——addLoadEvent函数

    先贴源码 function addLoadEvent(func) { var oldonload = window.onload; //存入当前onload事件 if(typeof window.on ...

  8. scrollMenu,一款可滚动的菜单插件 兼容pc和移动端

    这个菜单 有两种样式  , 也可以通过animate.css加不同的动画效果!滚动的方式也有两种   一种为通用的overflow,另外一种是better-scroll的滚动效果 在线链接地址  ht ...

  9. Oracle 使用命令导入dmp文件

    若要导入到特定的表空间则需要新建表空间,若不要求,则用已有的,则只需执行下面的步骤 在dos窗口中输入imp 用户名/密码@ip地址:端口号/数据库实例 file='需要导入的dmp文件的路径' fu ...

  10. etcd raft library设计原理和使用

    早在2013年11月份,在raft论文还只能在网上下载到草稿版时,我曾经写过一篇blog对其进行简要分析.4年过去了,各种raft协议的讲解铺天盖地,raft也确实得到了广泛的应用.其中最知名的应用莫 ...