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.

这道题让我们翻转字符串中的每个单词,感觉整体难度要比之前两道Reverse Words in a String IIReverse Words in a String要小一些,由于题目中说明了没有多余空格,使得难度进一步的降低了。首先我们来看使用字符流处理类stringstream来做的方法,相当简单,就是按顺序读入每个单词进行翻转即可,参见代码如下:

解法一:

class Solution {
public:
string reverseWords(string s) {
string res = "", t = "";
istringstream is(s);
while (is >> t) {
reverse(t.begin(), t.end());
res += t + " ";
}
res.pop_back();
return res;
}
};

下面我们来看不使用字符流处理类,也不使用STL内置的reverse函数的方法,那么就是用两个指针,分别指向每个单词的开头和结尾位置,确定了单词的首尾位置后,再用两个指针对单词进行首尾交换即可,有点像验证回文字符串的方法,参见代码如下:

解法二:

class Solution {
public:
string reverseWords(string s) {
int start = , end = , n = s.size();
while (start < n && end < n) {
while (end < n && s[end] != ' ') ++end;
for (int i = start, j = end - ; i < j; ++i, --j) {
swap(s[i], s[j]);
}
start = ++end;
}
return s;
}
};

类似题目:

Reverse Words in a String II

Reverse Words in a String

参考资料:

https://discuss.leetcode.com/topic/85773/nothing-fancy-straight-java-stringbuilder

https://discuss.leetcode.com/topic/85797/java-two-methods-3-line-using-built-in-and-char-array

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Reverse Words in a String III 翻转字符串中的单词之三的更多相关文章

  1. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 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 II 翻转字符串中的单词之二

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

  3. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

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

  4. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  5. LeetCode 557. Reverse Words in a String III (反转字符串中的单词 III)

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

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

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

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  8. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  9. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

随机推荐

  1. pl/sql进阶--例外处理

    在pl/sql的执行过程中发生异常时系统所作的处理称为一个例外情况(exception).通常例外情况的种类有三种: 1.预定义的oracle例外情况oracle预定义的例外情况大约有24个,对于这种 ...

  2. IntelliJ IDEA的入门使用

    1 修改背景颜色 点击File,Settings 2 修改字体格式,以及字体大小 我个人习惯Eclipse的默认字体,所有把字体修改成Eclipse默认的Consolas 同上点击File,Setti ...

  3. 北京工业大学耿丹学院2016下C作业学习总结

    北京工业大学耿丹学院2016下C的班级地址在https://edu.cnblogs.com/campus/bjgygd/Sixteen-One . 第一次作业:两部分 第一部分:新建博客,书写第一篇随 ...

  4. C程序第二次作业

    2-1删除字符串中数字字符 1.设计思路 (1)主要描述题目算法 第一步:遍历指针s所指的s数组. 第二步:如果 * (s+i)在0至9之间的话,则跳过此 * (s+i). 第三步:如果* (s+i) ...

  5. Android接受验证码自动填入功能(源码+已实现+可用+版本兼容)

    实际应用开发中,会经常用到短信验证的功能,这个时候如果再让用户就查看短信.然后再回到界面进行短信的填写,难免有多少有些不方便,作为开发者.本着用户至上的原则我们也应该来实现验证码的自动填写功能,还有一 ...

  6. 08-TypeScript中的类

    类的概念通常是在后端开发中实现的思想,比如C#.C++或Java,传统的JavaScript开发通过使用原型模式来模拟类的功能.在TypeScript中,天生就是支持类 的,可以让前端的开发更加具有面 ...

  7. 逆向集录_00_不同程序OEP特征总结

    在分析/逆向 程序时,如果事先知道这类程序的一些特征,那将会是事半功倍的: 分析/逆向 程序,和写程序不同,比喻的话:写程序像在作案,分析/逆向 程序就像是在破案,对破案来讲,重在假想和推理: 特征1 ...

  8. php实现单,双向链表,环形链表解决约瑟夫问题

    传智播客PHP学院 韩顺平 PHP程序员玩转算法第一季  http://php.itcast.cn 聊天篇: 数学对我们编程来说,重不重要? 看你站在什么样的层次来说. 如果你应用程序开发,对数学要求 ...

  9. js window

    window对象: browser object mode :bom对象. bom提供了独立于内容而与浏览器窗口进行交互的对象. bom主要用于管理窗口与窗口之间的通讯,因此其核心对象是window ...

  10. Python-迭代器&生成器&装饰器&软件目录结构规范-Day5

    目录Day-Python-迭代器&生成器 21.生成器 21.1.生成器引入 21.2.生成器作用 31.3.创建生成器的方法 31.4.用函数来实现复杂的生成器 51.5.把函数变成生成器通 ...