[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& ...
随机推荐
- linux_c_udp_example
udp_server #include <stdlib.h> #include <string.h> #include <unistd.h> #include &l ...
- missing KW_END at ')' near '<EOF>'
case when 没写 end
- matplotlib画图--Line Color
1.线形 2.标记 3.颜色
- SQLite数据库以及增删改查的案例
Android使用开源的与操作系统无关的SQL数据库——SQLite 一:在命令行下创建数据库: 1.启动模拟器后,打开命令行,执行adb shell 2.进入所在工程目录 3.执行sqlite3 m ...
- 牛客——Rabbit的字符串
题目: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 Rabbit得到了一个字符串,她的好朋 ...
- 两个exe共享内存数据
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- git clone与git pull区别
从远程服务器克隆一个一模一样的版本库到本地,复制的是整个版本库,叫做clone.(clone是将一个库复制到你的本地,是一个本地从无到有的过程)从远程服务器获取到一个branch分支的更新到本地,并更 ...
- 64位win7+PCL1.6.0+VS2010,64位win10+PCL1.6.0+VS2010
https://blog.csdn.net/liukunrs/article/details/80216329 大体转载自:https://blog.csdn.net/sinat_24206709/a ...
- awk grep sed 的一些问题
条件 匹配 打印含关键字的行 ps aux | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' z ...
- winter_holiday_2019
个人逐利对他人的影响:私营企业主的个人付出.作为长辈对晚辈的付出 是2020要实现精准扶贫吗?精准扶贫的概念,是针对一个家庭还是针对家里的每个人的平均收入.精准扶贫比教育更有作用,刘传铁(湖北省委高校 ...