【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 ...
随机推荐
- lua5.4 beta中的to-be-closed变量的用法
对应目前最新lua5.4 beta版本:2019-10-09发布 这个功能之前修改过两次语法,当前的语法不出意外将会是最终决定了,目前还没有最新的中文资料,所以我来这里发一下. 先介绍下这个功能: 被 ...
- 在idea的java开发中字符串length()方法获取长度与赋值不符的问题
最近在开发中用到length()方法获取中文字符串的长度,发现获得的长度与实际不符.比如个String类型赋值为"中",但获取长度却是2. 这让我百思不得其解,后来突然想起来我在研 ...
- JavaScript设计模式,单例模式!
单例设计模式:保证一个类仅有一个实例,并且提供一个访问它的全局访问点.有些对象只需要一个,这时可用单例模式. 传统的单例模式 和new 创建对象的调用不一样 调用者要调用xxx.getInstance ...
- 快速挂起VIM以及调出被挂起的VIM的方法
vim中开了多窗口后有时需要临时切出去执行shell指令,查看结果,在vim中用%很不方便查看结果,要切出去又要逐个小窗口:q,非常麻烦. 上网一查竟然有挂起的方法: 挂起:ctrl-z 调出:fg ...
- iOS 客户端获取七牛上传token
一.官方参考文档: 1.上传策略http://developer.qiniu.com/article/developer/security/put-policy.html 2.上传凭证(即uptoke ...
- 利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired
大家平时使用spring依赖注入,都是怎么写的? @Servicepublic class OrderService {@Autowiredprivate UserService userServic ...
- ebs 初始化登陆
BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...
- ubantu安装maven
下载地址 http://maven.apache.org/download.cgi 或直接命令行下载 wget https://downloads.apache.org/maven/maven-3/3 ...
- vue 第三方图标库
"font-awesome": "^4.7.0", "dependencies": { "axios": "^ ...
- MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
一.设置paramterType 1.类型为基本类型 a.代码示例 映射文件: <select id="findShopCartInfoById" parameterType ...