LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.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?
题解:
input变成 char array.
先reverse 全部 char array. 从头往后走,遇到空格或者array尾部,就reverse中间的单词.
Note: Do NOT forget i == n, reverse.
Time Compelxity: O(s.length()). Space: O(1).
AC Java:
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 = 1; 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 temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
跟上Reverse Words in a String III, Rotate Array.
LeetCode Reverse Words in a String II的更多相关文章
- [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 ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [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 ...
- 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 III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...
- [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 ...
随机推荐
- Storm可靠性实例解析——ack机制
对于Storm,它有一个很重要的特性:“Guarantee no data loss” ——可靠性 很显然,要做到这个特性,必须要track每个data的去向和结果.Storm是如何做到的呢——ack ...
- Extjs tree的相关方法及配置项
Ext.tree.TreePanel 主要配置项: root:树的根节点. rootVisible:是否显示根节点,默认为true. ...
- node.js 调用天气webservice接口
首先安装soap模块 npm install soap 1 2 3 4 5 6 7 8 9 10 var soap = require('soap'); var url = 'http://w ...
- HDU 2853 (KM最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题目大意:二分图匹配费用流.①最大匹配②最小原配变动 解题思路: 如果去掉第二个要求,那么就是裸 ...
- 20145308刘昊阳 《Java程序设计》实验五报告
20145308刘昊阳 <Java程序设计>实验五 Java网络编程及安全 实验报告 实验名称 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: ...
- android 内部缓存器(手机自带的存储空间中的当前包文件的路径)
关于Context中: 1. getCacheDir()方法用于获取/data/data/<application package>/cache目录 2. getFilesDir()方法用 ...
- [知识点]计算几何I——基础知识与多边形面积
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...
- win8.1上wamp环境中利用apache自带ab压力测试工具使用超简单讲解
2015.10.4apache自带ab压力测试工具使用:本地环境:win8.1 wampserver2.5 -Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 可以参考一 ...
- IOS启动顺序
一.UIApplicationMain的执行步骤1.创建一个UIApplication对象,一个程序对应一个UIApplication对象(单例),UIApplication对象是程序的象征2.接下来 ...
- Eclipse for Mac 常用快捷键
为了提高开发效率,Eclipse 为我们提供了许多快捷键,它们能够帮助我们快速和方便的完成一些繁琐的操作.在这里只提供 Eclipse for Mac 的常用快捷键. Command + O:显示大纲 ...