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 ...
随机推荐
- PHP初学[DAY2]
昨天安装了PHP的开发环境,根据一个百度经验里的介绍做的,可惜链接找不着了.目前状况是这样:在Apache24下有一个www的文件夹,通过编辑里边的index.php来学习PHP程序的编写,程序运行的 ...
- Javascript小括号“()”的多义性
摘要:本文主要介绍JavaScript中小括号有五种语义. Javascript中小括号有五种语义 语义1,函数声明时参数表 function func(arg1,arg2){ // ... } 语义 ...
- Android AIDL 进行进程间通讯(IPC)
编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...
- 用存储过程 将大段的SQL藏起来
在日常工作中,当面对比较复杂的数据库操作时不免要写一些比较长的SQL,由于某系SQL有些长(目前我写的最长的貌似有30多行吧),这时候长会面临这个 方法 优点 缺点 用"+"串 ...
- ffmpeg常用命令
windows http://www.cnblogs.com/wainiwann/p/4128154.html ubuntu http://blog.csdn.net/hellowxwworld/ar ...
- QSpinBox 和 QSlider 联合使用方法
在Qt中,有时候我们想要联合QSpinBox 和 QSlider,使得移动滑块,QSpinBox中的数据会变化,或者我们在QSpinBox中输入一个数值,响应的滑块也会变化,如下图所示:
- Hadoop_初识
一. 什么是Hadoop Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序. 充分利用集群的威力进行高速运算和存储. Hado ...
- JQuery小结
一.选择网页元素 jQuery的基本设计和主要用法,就是"选择某个网页元素,然后对其进行某种操作".这是它区别于其他函数库的根本特点. 使用jQuery的第一步,往往就是将一个选择 ...
- mysql5.7密码问题
安装: yum search mysql """mysql-server mysql mysql-devel mysql-community-server"&q ...
- Daily Scrum 11.2
由于11月1号是周六,小组里人不是很齐,所以Scrum会议暂停一次. 周日大家的工作都已经进入到尾声了,但是由于人员方面出现一些问题,界面方面做的还不到位.鉴于我们还只是完成了一个比较简单的工作,与真 ...