[LC] 151. Reverse Words in a String
Given an input string, reverse the string word by word.
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces. Solution 1:
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return "";
}
char[] charArr = s.toCharArray();
swap(charArr, 0, s.length() - 1);
int i = 0, start = 0;
while (i < s.length()) {
if (i == 0 || charArr[i - 1] == ' ') {
start = i;
}
if (i == charArr.length - 1 || charArr[i + 1] == ' ') {
swap(charArr, start, i);
}
i += 1;
}
// need to trim space inside
int slow = 0;
for (int j = 0; j < charArr.length; j++) {
if (charArr[j] == ' ' && (j == 0 || charArr[j - 1] == ' ')) {
continue;
}
charArr[slow++] = charArr[j];
}
return new String(charArr, 0, slow).trim();
}
private void swap(char[] charArr, int i, int j) {
while (i < j) {
char tmp = charArr[i];
charArr[i] = charArr[j];
charArr[j] = tmp;
i += 1;
j -= 1;
}
}
}
Solution 2:
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return "";
}
String[] strArr = s.split("\\s+");
StringBuilder sb = new StringBuilder();
for (int i = strArr.length - 1; i >= 0; i--) {
sb.append(strArr[i] + " ");
}
return sb.toString().trim();
}
}
[LC] 151. Reverse Words in a String的更多相关文章
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【刷题-LeetCode】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- (String)151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Java for LeetCode 151 Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- leetcode 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
随机推荐
- quartz详解4:quartz线程管理
http://blog.itpub.NET/11627468/viewspace-1766967/ quartz启动后有多个线程同时在跑.启动时会启动主线程.集群线程.检漏线程.工作线程.主线程负责查 ...
- zookeeper基础教程
一.关于zookeeper Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储, Zookeeper 作用 ...
- POJ 1125:Stockbroker Grapevine
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64 ...
- python全局变量、回调函数
1.python全局变量相关概念及使用 来自菜鸟教程上的例子: http://www.runoob.com/python3/python3-function.html 一.python入参需要注意地方 ...
- Java编程知识点梳理
1. elementAt() temp.elementAt(0) 返回temp这个vector里面存放的第一个元素--->也是一个vector类型. 2. 字符串空格分割 String [] ...
- PIP一次性导入所有环境和指定镜像源
镜像源: 阿里云:https://mirrors.aliyun.com/pypi/simple/豆瓣:https://pypi.douban.com/simple/清华大学:https://pypi. ...
- Unity3D一些基本的概念和一些基本操作
场景:整个游戏由场景组成,一个游戏至少要有一个场景,如果把所有的游戏画面放在一个场景里也是可以的,如果游戏非常非常的大,如果所有的东西都放到一个场景里那么结构就不是那么清晰了而且处理起来就会麻烦一些, ...
- cmd 进入指定文件夹
1.通常情况下,我们要进入其他盘符下的任意目录,需要在CMD窗口运行两次命令:第一次,进入盘符,第二次进入指定目录 #进入D盘 d: #进入D盘下的anaconda目录 cd anacond 2.通过 ...
- SQL基础教程(第2版)第5章 复杂查询:5-2 子查询
第5章 复杂查询:5-2 子查询 ● 一言以蔽之,子查询就是一次性视图( SELECT语句).与视图不同,子查询在SELECT语句执行完毕之后就会消失.● 由于子查询需要命名,因此需要根据处理内容来指 ...
- bfs--P1443 马的遍历
有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 跟迷宫一样,找最近距离,显然用bfs,两个方位数组dir1和dir2用来表示 ...