题目:

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. Canvas学习笔记——拖曳与投掷物体

    首先用一个例子来演示这个效果: 鼠标可以拖曳和投掷小球   // > 16 & 0xff, g = color >> 8 & 0xff, b = color > ...

  2. mysql连接超时的问题

    使用Hibernate + MySQL数据库开发,链接超时问题: com.mysql.jdbc.CommunicationsException: The last packet successfull ...

  3. MySQL 启动报错:File ./mysql-bin.index not found (Errcode: 13)

    Linux下安装初始化完MySQL数据库之后,使用mysqld_safe启动mysql数据库,如下发现,启动失败 [root@SVNServer bin]# ./mysqld_safe –user=m ...

  4. OpenCV 环境搭建( Win7 32位 / VS2010 / OpenCV2.4.8 )

    前言 本文介绍如何搭建 OpenCV 开发环境 配置如下: 操作系统:WIN7 32位 开发平台:VS2010 OpenCV 版本:2.4.8 第一步:安装 OpenCV 2.4.8 1. 登陆 Op ...

  5. EasyDarwin开源流媒体服务器如何实现按需推送直播的

    --本文转自EasyDarwin开源团队成员邵帅的博客:http://blog.csdn.net/ss00_2012/article/details/51441753 我们使用EasyDarwin的推 ...

  6. 从TFS中的现有项目复制一份作为新项目,导致提交的服务器无法加载

    解决方案: 1.编辑 .csproj文件,改为自己的名字 2.取消解绑

  7. ABAP OLE常用方法和属性

    转自 http://www.cnblogs.com/eric0701/p/5213694.htmlSAP EXCEL OLE常用方法和属性 附加网上找到的比较好的源代码示例一份 1.ole中如何保存和 ...

  8. 使用Scapy回放报文pcap

    一.准备环境: Ubuntu + python2.7 sudo apt-get install python-scapy   二.准备报文: 先抓取一些报文,本实验使用的是DHCP的报文. 文件-导出 ...

  9. 远程请求json数据,list中显示

    public class MainActivity extends Activity { protected static final int WHAT_REQUEST_SUCCESS = 1; pr ...

  10. linux内核container_of宏定义分析

    看见一个哥们分析container_of很好,转来留给自己看 一.#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMB ...