LeetCode: 557Reverse Words in a String III(easy)
题目:
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)的更多相关文章
- [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 ...
- LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...
- 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 ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 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 ...
- [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 ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- 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 ...
随机推荐
- maven的坑: Exception in thread "pool-1-thread-1" java.lang.NoClassDefFoundError: org/eclipse/aether/spi/connector/Transfer$State
搭建ReboletricSample的环境: 搭建完成,执行的时候报错: Exception in thread "main" java.lang.NoClassDefFoundE ...
- Eclipse快捷键大全(转载)
一·eclipse 中的一些常用的快捷键Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] ...
- IO复用之select实现
前言 在看过前文:初探IO复用后,想必你已对IO复用这个概念有了初步但清晰的认识.接下来,我要在一个具体的并发客户端中实现它( 基于select函数 ),使得一旦服务器中的客户进程被终止的时候,客户端 ...
- three supported reliability levels: * End-to-end * Store on failure * Best effort
https://github.com/cloudera/flume/blob/master/flume-docs/src/docs/UserGuide/Introduction === Reliabi ...
- Ubuntu搜狗输入法候选词乱码
打开Fcitx Configuration,进入Addon标签 选中Simplified Chinese To Traditional Chinese Convert Simplified ...
- 【NOIP 模拟赛】 道路
题目描述在二维坐标平面里有 N 个整数点,信息班某一巨佬要访问这 N 个点.刚开始巨佬在点(0,0)处. 每一步,巨佬可以走到上.下.左.右四个点.即假设巨佬当前所在点的坐标是(x,y),那么它下一步 ...
- SE14 调整并激活数据库
一.使用SE14 激活并调整MARA 时的消息:(体验心惊肉跳,你就改改标准表试试.-增强也请小心谨慎) 使用检查: 调整并激活后的结果: 二.如果 运行时对象检查 出错误,参考 http://blo ...
- 【 spring配置文件详解】
转自: http://book.51cto.com/art/201004/193743.htm Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的 ...
- centos6.5安装jdk
1.查看Linux环境自带JDK 使用命令:# rpm -qa|grep gcj 显示内容其中包含相应信息# java-x.x.x-gcj-compat-x.x.x.x-xxjpp# java-x.x ...
- slim.flatten——将输入扁平化但保留batch_size,假设第一维是batch
slim.flatten(inputs,outputs_collections=None,scope=None) (注:import tensorflow.contrib.slim as slim) ...