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. 高性能集群软件keepalived

     Keepalived介绍 以下是keepalive官网上的介绍.官方站点为http://www.keepalived.org.         Keepalived is a routing sof ...

  2. Neo4j--图形理论基础

    参考 https://www.w3cschool.cn/neo4j/neo4j_graph_theory_basics.html 节点 圆圈表示的是节点.节点是图表的基本单位. 节点可以用来存储数据, ...

  3. Git--记一次丢失本地记录但是代码已提交到gerrit

    参考 https://blog.csdn.net/yucendulang/article/details/76199913 https://stackoverflow.com/questions/28 ...

  4. StarUML类图相关——关联、聚合、组合、泛化、依赖、实现

    在阅读设计模式相关的书籍,或者其他一些项目.相关博客等等,经常会遇到类图,它对于一个类的信息,如变量.方法及其可见性,类与类(接口)之间的继承关系.依赖关系.聚合关系.组合关系等,都可以比较形象得当地 ...

  5. chr()//ord() //进制转换函数//eval()//文件函数//split()

    1.chr() 函数 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 用法:chr(i) i可以是10进制也可以是16进制的形式的数字. 2.or ...

  6. 吴裕雄--天生自然 PHP开发学习:超级全局变量

    <!DOCTYPE html> <html> <body> <?php $x = 75; $y = 25; function addition() { $GL ...

  7. vue拖拽插件(弹框拖拽)

    // =======拖拽 插件 cnpm install vuedraggableimport draggable from 'vuedraggable' <draggable v-model= ...

  8. Android studio中2种build.gradle文件介绍

    根目录下的build.gradle通常不需要修改这个文件中的内容,除非需要添加一些全局的项目构建配置 buildscript { repositories { google() //声明代码托管仓库G ...

  9. php多态模拟

    在PHP中,多态是最常用到的一种特性.所谓多态,是指同一个东西不同形态的展示.在PHP中,我们这样定义多态,一个类被多个子类继承,如果这个类的某个方法在多个子类中表现不同的功能,那么这种行为我们就称其 ...

  10. TF分布式问题

    碰到一个没解决的问题. 用tensorflow 分布式异步更新模式训练模型, 模型中带正则项, 每个batch的损失函数为 \[\lambda \|W\|_1 + \frac 1 {N_j} \sum ...