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 ...
随机推荐
- NHibernate 中删除数据的几种方法
今天下午有人在QQ群上问在NHibernate上如何根据条件删除多条数据,于是我自己就写了些测试代码,并总结了一下NHibernate中删除数据的方式,做个备忘.不过不能保证囊括所有的方式,如果还有别 ...
- BZOJ3934 : [CQOI2015]标识设计
轮廓线插头DP. 设$f[i][j][a][b][c][d][e]$表示考虑到了$(i,j)$,轮廓线上3个下插头的位置分别为$a,b,c$,是否有右插头,已经放了$e$个$L$的方案数. 然后直接D ...
- BZOJ3759: Hungergame 博弈论+线性基
学了新的忘了旧的,还活着干什么 题意:一些盒子,每步可选择打开盒子和取出已打开盒子的任意多石子,问先手是否必胜 搬运po姐的题解: 先手必胜的状态为:给出的数字集合存在一个异或和为零的非空子集,则先手 ...
- Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误
解决方案如下:将tomcat下的server.xml文件中的端口有问题,修改规则按以下标准显示“http的端口修改为6000 to 6800之间,shutdown的端口修改为3000 to 3300之 ...
- 初识WebSocket
众所周知,Http协议是无状态的,并且是基于Request/Response的方式与服务器进行交互,也就是我们常说的单工模式.但是随着互联 网的发展,浏览器与服务端进行双向通信需求的增加,长轮询向服务 ...
- django数据库动态添加列
方法一: python manage.py migrate 方法二: python manage.py migrate 效果: ---〉
- mysql修改数据表名
在使用mysql时,经常遇到表名不符合规范或标准,但是表里已经有大量的数据了,如何保留数据,只更改表名呢? 可以通过建一个相同的表结构的表,把原来的数据导入到新表中,但是这样视乎很麻烦. 能否简单使用 ...
- zk编程语言: 如何改变datebox框值的大小及高度
<?page title="" contentType="text/html;charset=UTF-8"?> <zk > <st ...
- Greenplum 生成加分区语句
在使用greenplum中会使用分区表,但同时分区表需要维护分区:比如加分区,这个过程比较痛苦,查询相关资料以后有了相应的解决办法,但是该办法也不是万能的,有诸多限制,关于限制有兴趣的同学可以查看我文 ...
- 温故而知新,jquery选择器$=
$("select[name$=Sort]") 可以选择到如下元素: <select class="sort-selec" name="eton ...