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-space characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?
链接: http://leetcode.com/problems/reverse-words-in-a-string-ii/
题解:
翻转单词II。这次给定了char array,我们就可以使用三步翻转法了。因为题目的条件很优惠,前后没有空格,单词间又只有一个空格,那我们可以省略很多边界条件的判定,先翻转整个数组,然后碰到单词就正序回来。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public void reverseWords(char[] s) {
if(s == null || s.length == 0)
return;
reverse(s, 0, s.length - 1);
int lo = 0;
for(int i = 0; i <= s.length; i++) {
if(i == s.length || s[i] == ' ') {
reverse(s, lo, i - 1);
lo = i + 1;
}
}
}
private void reverse(char[] s, int i, int j) {
while(i < j)
swap(s, i++, j--);
}
private void swap(char[] s, int i, int j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
题外话:
同事要去南极玩,先飞到阿根廷,再到乌斯怀亚,然后坐科考船过去。科考船一套大概10500刀,机票另算,好爽啊。以后我也要好好去旅游。
二刷:
跟一刷基本一样
Java:
public class Solution {
public void reverseWords(char[] s) {
if (s == null || s.length < 2) return;
int len = s.length;
reverse(s, 0, len - 1);
int lo = 0;
for (int i = 0; i < len; i++) {
if (s[i] == ' ') {
reverse(s, lo, i - 1);
lo = i + 1;
}
}
reverse(s, lo, len - 1);
}
private void reverse(char[] s, int lo, int hi) {
while (lo < hi) {
char tmp = s[lo];
s[lo] = s[hi];
s[hi] = tmp;
lo++;
hi--;
}
}
}
Reference:
http://www.zhihu.com/question/19857821
186. Reverse Words in a String II的更多相关文章
- [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 ...
- 186. Reverse Words in a String II 翻转有空格的单词串 里面不变
[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...
- [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 ...
- Reverse Words in a String I & Reverse Words in a String II
Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [Swift]LeetCode186. 翻转字符串中的单词 II $ 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 ...
随机推荐
- jLink(v8)GDB 命令总结
/** ****************************************************************************** * @author Maox ...
- PHP 魔术方法 __sleep __wakeup(四)
串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输. 然后再反串行化还原为原来的数据. 你在反串行化类的对象之前定义的类,PHP ...
- JAVA MemCache 史无前例的详细讲解【转】
非原创转自:http://nhy520.iteye.com/blog/1775893 这篇文章是我看到的介绍的比较详细的,入门级别算是足足够了 Memcach什么是Memcache Memcache集 ...
- 汇编语言-打印部分ASCII表
用表格形式显示字符 1. 题目:用表格形式显示ASCII字符 2.要求:按15行×16列的表格形式显示ASCII码为10H-100H的所有字符,即以行为主的顺序及ASCII码递增的次序依次显示对应的字 ...
- java实现多模匹配算法
这个是好几年前写的了.都统一放到cnblogs上面. --------------------------------Node ---------------------------------- p ...
- 【转】给Winform的button等控件添加快捷键
ref: http://blog.sina.com.cn/s/blog_4cb9953f0100cy4z.html 第一种:Alt + *(按钮快捷键) 在大家给button.label.menuSt ...
- 【原】Oracle查询指定表里的触发器
select * from all_triggers WHERE table_name='表名'
- Nodejs加密php解密
var crypto = require('crypto'); function decode(cryptkey, iv, secretdata) { var decipher = crypto.cr ...
- tomcat源码解读(1)–tomcat热部署实现原理
tomcat的热部署实现原理:tomcat启动的时候会有启动一个线程每隔一段时间会去判断应用中加载的类是否发生变法(类总数的变化,类的修改),如果发生了变化就会把应用的启动的线程停止掉,清除引用,并且 ...
- 解析php file_exists无效的解决办法
php中file_exists无效的解决办法. 方法1 :据官方手册上描述若php教程的safe mode相关的设置过于苛刻,就会出现这样的情形:尽管文件真实存在也被误报,认为文件不存在. 由于服务器 ...