【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
目录
题目地址: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:
- A word is defined as a sequence of non-space characters.
- The input string does not contain leading or trailing spaces.
- 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++)的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 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 ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 186. Reverse Words in a String II 翻转有空格的单词串 里面不变
[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
随机推荐
- MySQL_集群
管理节点:192.168.31.66 sql节点1+data1节点:192.168.31.42 sql节点2+data2节点:192.168.31.128 llll
- nginx_install
[root@MiWiFi-R1CM-srv ~]# yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel pcre-devel pc ...
- 2 — springboot的原理
1.初步探索:第一个原理:依赖管理 发现:这里面存放着各种jar包 和 版本号 这也是:我们在前面第一个springboot项目创建中勾选了那个web,然后springboot就自动帮我们导入很多东西 ...
- Flink(五) 【消费kafka】
目录 0.目的 1.本地测试 2.线上测试 提交作业 0.目的 测试flink消费kafka的几种消费策略 kafkaSource.setStartFromEarliest() //从起始位置 kaf ...
- Spark(十一)【SparkSQL的基本使用】
目录 一. SparkSQL简介 二. 数据模型 三. SparkSQL核心编程 1. IDEA开发SparkSQL 2. SparkSession 创建 关闭 获取SparkContext 3. D ...
- java.sql.SQLException: Cannot create com._51doit.pojo.User: com._51doit.pojo.User Query: select * from user where username = ? and password = ? Parameters: [AA, 123]
在从数据库中查询数据,并存入javabean中去时,报这个错误 原因:在建立User类去存储信息时没有创建无参构造方法,创建一个无参构造方法即可
- 简化版chmod
我们知道对文件访问权限的修改在Shell下可通过chmod来进行 例如 可以看到v.c文件从无权限到所有者可读可写可执行.群组和其他用户可读可执行 chmod函数原型 int chmod(const ...
- Android Handler 消息机制原理解析
前言 做过 Android 开发的童鞋都知道,不能在非主线程修改 UI 控件,因为 Android 规定只能在主线程中访问 UI ,如果在子线程中访问 UI ,那么程序就会抛出异常 android.v ...
- C++ 害死人不偿命的(3n+1)猜想
第一次刷PAT ,注意事项:就像普通编译器一样要导入头文件 还有 using namespace std:要不然会报错(鬼知道我经历了什么 微笑.jpg) 1 #include <iostrea ...
- SVN终端演练(个人开发\多人开发)
SVN终端演练(个人开发) ### 1. 命令格式 命令行格式: svn <subcommand> [options] [args] svn 子命令 [选项] [参数] ...