题目:

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. vue 路由组件不重新加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. php逻辑操作符中&和&&的异同

    php有5种算术操作符(+ - * / %),6种赋值操作符(+= -= *= /= %= .=),8种比较操作符(=== < > <= >= != <> !==) ...

  3. jquery 使用ajax,正常返回后,不执行success的问题

    背景: 在使用到jQuery的ajax时,如果指定了dataType为json,老是不执行success回调,而是执行了error回调函数. 原因: 然后继续下载了几个jquery版本,如1.3.2, ...

  4. java基础知识查漏 三

    一.Servlet 和Jsp的生命周期 1.Servlet生命周期       Servlet是运行在Servlet容器(有时候也叫Servlet引擎,是web服务器和应用程序服务器的一部分,用于在发 ...

  5. js复杂数据格式提交

    有的时候额后台需要一个对象Map值,如{name: '姓名',attributeMap:{skill: '名称;checkbox;true;&篮球:1,羽毛球:2',name:'lsg' }} ...

  6. holiday和vacation的区别

    holiday:假日vacation:假期a.对于英国人或者澳大利亚人来说,“假日”的意思等同于“假期”(尽管他们很少用“假期”)b.如果你是美国人,“假日”是指一个特殊的日子,好像圣诞节,而“假期” ...

  7. 查询所有联系人并选中显示 contentprovider

    <!-- 读取联系人记录的权限 --> <uses-permission android:name="android.permission.READ_CONTACTS&qu ...

  8. 盒子的display属性

    <body> <div style="display:inline">Box-1</div> <div style="displ ...

  9. vue的缓存机制

    缓存,不管是PC 端还是移动端,不可避免的问题.vue中有一个keepAlive,这个api 基本 能实现我们开发的一些需要. 一.简单介绍下keep-alive: 1.把切换出去的组件保留在内存中, ...

  10. 人生苦短之Python列表拷贝

    列表拷贝的几种方法:    1.工厂函数 b=list(a) >>> a=[1,2,3,4] >>> b=list(a) >>> b [1, 2, ...