题目地址:https://leetcode-cn.com/problems/reverse-words-in-a-string-ii/

题目描述

Given an input string , reverse the string word by word.

Example:

Input:  ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]

Note:

  1. A word is defined as a sequence of non-space characters.
  2. The input string does not contain leading or trailing spaces.
  3. The words are always separated by a single space.

Follow up: Could you do it in-place without allocating extra space?

题目大意

给定一个字符串,逐个翻转字符串中的每个单词。

解题方法

每个单词单独翻转+总的翻转

没记错的话是剑指offer上的题目,做法是用到了一个公式c b a = (aT bT cT)T,如果知道这个公式应该很好办了。

为什么需要公式而不是直接找到首尾单词互换位置呢?很容易看出每个单词的长度是不同的,互换位置可能会覆盖其他的单词。

C++代码如下:

class Solution {
public:
void reverseWords(vector<char>& s) {
if (s.empty()) return;
int pre = 0;
int cur = 0;
while (cur <= s.size()) {
if (cur == s.size() || s[cur] == ' ') {
reverse(s, pre, cur - 1);
pre = cur + 1;
}
cur ++;
}
reverse(s, 0, s.size() - 1);
}
// reverse [start, end]
void reverse(vector<char>& s, int start, int end) {
for (int i = 0; i <= (end - start) / 2; ++i) {
swap(s[start + i], s[end - i]);
}
}
};

日期

2019 年 9 月 22 日 —— 熬夜废掉半条命

【LeetCode】186. Reverse Words in a String II 解题报告 (C++)的更多相关文章

  1. [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 ...

  2. leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

    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

    题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...

  4. LeetCode 557 Reverse Words in a String III 解题报告

    题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...

  5. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

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

  7. 【LeetCode】541. Reverse String II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  8. 186. Reverse Words in a String II 翻转有空格的单词串 里面不变

    [抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...

  9. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

随机推荐

  1. 如何使用scp在Linux服务器的后台传输文件?

    目录 一.上传 常规操作 建议 后台运行 二.下载 两台服务器间文件如何传输?对于小文件,可以先从Linux服务器传到window,再传到另一台服务器.对于大的文件,如测序数据.比对文件等.这样的方法 ...

  2. 67-Gray Code

    Gray Code My Submissions QuestionEditorial Solution Total Accepted: 60277 Total Submissions: 165212 ...

  3. rabbitmq部署问题

    启动rabbitmq服务时报错: systemctl status rabbitmq-server 状态显示:Failed to start RabbitMQ broker Failed to sta ...

  4. perl中tr的用法(转载)

    转载:http://blog.sina.com.cn/s/blog_4a0824490101hncz.html (1)/c表示把匹配不上的字符进行替换. $temp="AAAABCDEF&q ...

  5. 深入理解动态规划DP

    通过最近对于一些算法题的思考,越来越发现动态规划方法的在时间上高效性,往往该问题可以轻松的找到暴力破解的方法,其时间复杂度却不尽人意.下面来看看几个常见的动态规划思路的经典问题 例一.有一段楼梯有10 ...

  6. linux系统中安装MySQL

    linux系统中安装MySQL 检查原来linux系统中安装的版本 rpm -qa | grep mysql 将其卸载掉 以 mysql-libs-5.1.71-1.el6.x86_64 版本为例 r ...

  7. A Child's History of England.44

    At this period of his reign, when his troubles seemed so few and his prospects so bright, those dome ...

  8. tomcat拦截特殊字符报400,如 "|" "{" "}" ","等符号的解决方案

    最近在做一个项目,需要对外暴露两个接口接收别人给的参数,但是有一个问题就是对方的项目是一个老项目,在传参数的时候是将多个字符放在一个参数里面用"|"进行分割,然而他们传参数的时候又 ...

  9. day06 视图层

    day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...

  10. day12 查找文件

    day12 查找文件 find命令:查找文件 find命令:在linux系统中,按照我们的要求去查询文件. 格式: find [查询的路径] [匹配模式] [匹配规则] 匹配模式: -name : 按 ...