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. 移动端APP CSS Reset及注意事项CSS重置

    @charset "utf-8"; * { -webkit-box-sizing: border-box; box-sizing: border-box; } //禁止文本缩放 h ...

  2. final用法

    1.修饰类 如果一个类被定义为final类型,那么该类无法被其他类继承,该类中的所有方法都是final类型的,字段是否是final类型取决于字段自身的定义. 2.修饰方法 一个方法被定义为final类 ...

  3. webpack前端工程化构建工具的使用

    一.模块打包机 1.创建文件 在目标文件下建立一个src文件夹作为js代码区:作为例子,我创建了两个js文件,并利用commonJS规范require引入到index.js中: moduleA.js: ...

  4. php数组根据某键值,把相同键值的合并最终生成一个新的二维数组

    <?php $a=array( '0'=>array( 'id'=>'1', 'names'=>'jack', '0'=>'sendone' ), '1'=>arr ...

  5. [leetcode-583-Delete Operation for Two Strings]

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  6. 【Android Developers Training】 90. 序言:解决云储存冲突

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. spring的Convert机制

    spring.core包内有Converter接口,方法是T convert(S source);从一个类型转为内容一个类型的实际转化器:converters可能非常多,需要一个注册器来集中管理使用, ...

  8. Example016实现下拉框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 如何使用mybatis对mysql数据库进行操作,batis的增删改查

    1.先下载Mybatis和mysql connecrt的jar包 下载地址: 链接: https://pan.baidu.com/s/1kVFfF8N 密码: ypkb 导入jar包,maven的话可 ...

  10. Jquery-鼠标事件

    鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的.(1):click事件:click事件于用户在元素敲击鼠标左键,并在相同元素上松开左键时触发.        $('p').click(fu ...