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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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& ...

  4. 【刷题-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: ...

  5. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

  6. (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& ...

  7. 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 ...

  8. 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& ...

  9. 151. Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

随机推荐

  1. 启动mysql遇到问题Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    在mysql的启动过程中有时会遇到下述错误 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 请问mys ...

  2. MessageBox.Show的使用

    MessageBox.Show("内容","标题") // 摘要:// 使用指定的帮助文件.HelpNavigator 和帮助主题显示一个具有指定文本.标题.按 ...

  3. Spring Boot Actuator Endpoints

    常用内建的Endpoints: beans:显示当前Spring应用上下文的Spring Bean完整列表(包含所有ApplicationContext的层次) conditions:显示当前应用所有 ...

  4. Neo4j--节点的增删查改基本用法

    注 node-name 和  label-name node-name 有点句柄的味道. 从面向对象来理解,label-name相当于一个类,node-name相当于这个类的对象. 类比关系型数据库的 ...

  5. centos socket通信时 connect refused 主要是防火墙问题

    centos socket通信时 connect refused 主要是防火墙问题,可以关闭防火墙,或者开放程序中的端口

  6. CF #610Div2 B2.K for the Price of One (Hard Version) (dp解法 && 贪心解法)

    原题链接:http://codeforces.com/contest/1282/problem/B2题目大意:刚开始有 p 块钱,商店有 n 件物品,你每次可以只买一件付那一件的钱,也可以买 k 件只 ...

  7. ubuntu root用户下找不到环境变量解决办法

    打开 gedit /root/.bashrc   ,在文件的末尾添加: source /etc/profile 然后执行更新:source /root/.bashrc

  8. String,StringBuffer与StringBuilder的区别与选择

    三者的区别 String:不可变类,一旦一个对象被建立的时候,包含在这个对象中的字符串序列是不可变的,直到这个对象被销毁.StringBuffer:可变字符序列的字符串.当其对象被创建的时候,可以用a ...

  9. java 面向对象概述, 函数解析

    函数:class FunctionDemo { public static void main(String[] args) { /* int x = 4; System.out.println(x* ...

  10. Django的URL路由基础

    一.概述 URL路由在Django项目中的体现就是urls.py文件,这个文件可以有很多个,但绝对不会在同一目录下.实际上Django提倡项目有个根urls.py,各app下分别有自己的一个urls. ...