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 ...
随机推荐
- Android Immersive Mode (沉浸模式) 还是 Translucent Bars (透明状态栏)
Immersive Mode (沉浸模式) 还是 Translucent Bars (透明状态栏) [科普]什么叫真正的“沉浸式”状态栏? 为什么在国内会有很多用户把「透明栏」(Translucent ...
- [Leetcode] Word BreakII
Question: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence w ...
- 1301. The Trip
A number of students are members of a club that travels annually to exotic locations. Their destinat ...
- css3+js打造炫酷图片展示
<!DOCTYPE html> <html onselectstart="return false"> <!-- onselectstart=&quo ...
- mvc2 To 4
asp.net mvc2新特性:1.区域,有利于分模块开发 2.数据注解和验证 3.View层强类型辅助方法 4.UI Templates 5.httppost,默认参数asp.net mvc3新特性 ...
- Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean
spring集成hibernate由两种形式 1.继续使用Hibernate的映射文件*.hbm.xml 2.使用jpa形式的pojo对象, 去掉*.hbm.xml文件 一.继续使用Hibernate ...
- 五、点数器《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...
- PL/SQL查询oracle数据库对象
dictionary 全部数据字典表的名称和解释,它有一个同义词dict,dict_column 全部数据字典表里字段名称和解释 如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句: se ...
- uboot中添加FIQ中断及相关问题
本文主要说明了在uboot中添加FIQ中断时遇到的问题以及对应的解决办法. 首先交代一下项目的软硬件环境.硬件方面,使用s3c2440作为主控芯片,外接串口.网卡等设备.软件方面,主控芯片上电后运行u ...
- The Unsolvable Problem
The Unsolvable Problem 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=45783 题意: ...