题目:

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) {
stack<char> tem;
string result;
string s1 = s + ' ';
for (auto c : s1){
if ( c != ' ')
tem.push(c);
else {
while(!tem.empty()){
result = result + tem.top();
tem.pop();
}
result = result + ' ';
}
}
result = result.substr(, result.length()-);
return result;
}
};

运行时间:772ms

别人的:

 class Solution {
public: string reverseWords(string s) {
string result(s.length(), );
int start = ;
int end = s.find();
while (end != -) {
for (int i = start; i < end; ++i)
result[end - (i - start) - ] = s[i];
start = end + ;
end = s.find(, start);
}
end = s.length();
for (int i = start; i < end; ++i)
result[end - (i - start) - ] = s[i];
return result;
}
};

运行时间:26mm

栈比较耗时间?

LeetCode: 557Reverse Words in a String III(easy)的更多相关文章

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

  2. LeetCode Reverse Words in a String III

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...

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

  4. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  5. 【leetcode】557. Reverse Words in a String III

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

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

  7. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  8. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

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

随机推荐

  1. 记录-外挂recovery的制作(魅蓝note)

    安卓的开源使其具有很强的可定制性,对于用户来说很具有可玩性.玩机一般来说就是解锁BootLoader刷入第三方recovery,利用第三方recovery刷第三方ROM,刷supersu获取root权 ...

  2. 谈谈 T 型人才

    谈谈 T 型人才   昨天的图片发模糊了,正好我把这个话题展开聊一聊吧.这个话题是关于复合型人才的,我把它称作 T 型人才. 「全栈」工程师 前一段时间,「全栈」工程师的概念很火,不过大多数时候,「全 ...

  3. Oracle操作笔记

    1.查询Oracle版本,数据库的SID select * from v$version; select name from v$database; 2.查询Oracle数据库所支持的功能 SELEC ...

  4. Redis 过期键的设置、获取和删除过期时间

    Redis 过期键的设置.获取和删除过期时间 转自http://blog.51cto.com/littledevil/1813956 设置过期 默认情况下键是没有生存时间的,也就是永不过期,除非清空内 ...

  5. 【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)

    博文主要内容有: 1.softmax regression的TensorFlow实现代码(教科书级的代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3 ...

  6. centos7设置tomcat7为系统服务的方法

    1,准备工作: JKD:jdk-7u72-Linux-x64.gz Tomcat:apache-tomcat-7.0.70.tar.gz OS:CentOS linux release 7.2.151 ...

  7. Vue入门(一) 环境配置

    Node.js 安装,https://nodejs.org/en/  默认安装就可以 安装好后测试版本,cmd  键入命令 1.node -v 2.npm -v 安装,淘宝 NPM         n ...

  8. go签名算法设计

    Go by Example 中文:Base64编码 https://books.studygolang.com/gobyexample/base64-encoding/

  9. spark 划分stage Wide vs Narrow Dependencies 窄依赖 宽依赖 解析 作业 job stage 阶段 RDD有向无环图拆分 任务 Task 网络传输和计算开销 任务集 taskset

    每个job被划分为多个stage.划分stage的一个主要依据是当前计算因子的输入是否是确定的,如果是则将其分在同一个stage,从而避免多个stage之间的消息传递开销. http://spark. ...

  10. Activity和ListActivity的区别

    http://book.51cto.com/art/201007/212051.htm