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 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的更多相关文章
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 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】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 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 ...
- 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_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- require.js的初步认识
我们之前呢写Javascript代码时都会写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多必须分成多个文件,依次加载.就如下面的代码: <script src="a.js ...
- python cookbook第三版学习笔记 一
数据结构 假设有M个元素的列表,需要从中分解出N个对象,N<M,这会导致分解的值过多的异常.如下: record=['zhf','zhf@163.com','775-555-1212','847 ...
- 就是要你懂Java中volatile关键字实现原理
原文地址http://www.cnblogs.com/xrq730/p/7048693.html,转载请注明出处,谢谢 前言 我们知道volatile关键字的作用是保证变量在多线程之间的可见性,它是j ...
- kaggle
注册kaggle可真所谓费劲心思,先是邮箱验证不来,换了两三个浏览器都不成功,非常恼火,没有验证码,最后还是FQ加谷歌浏览器,哎,注册之旅还是非常坎坷德,但是好消息是注册成功了.接下来是机器学习语言, ...
- TortoiseGit保存密码
TortoiseGit保存密码 方法一:使用Bash命令 1.设置name和emailgit config --global user.name "yilei"git config ...
- 我的学习之路_第二十章_JDBC
JDBC 使用JDBC技术,通过mysql提供的驱动程序,操作数据库 ● 1. 注册驱动 告知jvm 使用的是什么驱动程序(mysql,oracle) 使用API中的类 DriverManager中的 ...
- C++ inline函数与编译器设置
1. 经过测试#define与inline的速度几乎没有区别. 2. inline函数更为安全,有效避免了#define二义性问题.inline是真正的函数,而#define只是在字符串意义上的宏替换 ...
- 【PHP】震惊,一张图详解递归函数!!!!
在PHP学习中,递归函数是一个非常重要也是非常难以理解的部分,本博文将通过一张图尽可能演示这个过程,不对之处还请指出
- Java 获取字符串Hash值
Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for ...
- WebAssembly:随风潜入夜
What? WebAssembly 是一种二进制格式的类汇编代码,可以被浏览器加载和并进一步编译成可执行的机器码,从而在客户端运行.它还可以作为高级语言的编译目标,理论上任何语言都可以编译为 WebA ...