Given an input string, reverse the string word by word.

For example,

Given s = "the sky is blue",

return "blue is sky the".

click to show clarification.

Clarification:

  • What constitutes a word?

    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?

    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?

    Reduce them to a single space in the reversed string.
package com.liuhao.acm.leetcode;

/**
* @author liuhao
*
* Given an input string, reverse the string word by word. For example,
* Given s = "the sky is blue", return "blue is sky the".
*/
public class ReverseWords { public static String reverseWords(String s) { // 若输入字符串直接是空串。则直接返回该字符串
if (s.equals("")) {
return "";
} // 将字符串按空格"\\s{1,}"进行切割,一个或者多个空格的正则:"\\s{1,}"
String[] strArr = s.split("\\s{1,}");
int len = strArr.length; // 切割后字符串变成空串,直接返回
if (len == 0) {
return "";
} // 用StringBuilder更加有效
StringBuilder sb = new StringBuilder(""); // 反向输出之前切割的字符串数组
for (int i = len - 1; i >= 0; i--) {
if (!strArr[i].equals("")) {
sb.append(strArr[i]);
sb.append(" ");
}
} sb.deleteCharAt(sb.lastIndexOf(" ")); return sb.toString();
} public static void main(String[] args) {
System.out.println(reverseWords(""));
} }

【LeetCode刷题Java版】Reverse Words in a String的更多相关文章

  1. 【leetcode刷题笔记】Reverse Words in a String

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

  2. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

  3. 【leetcode刷题笔记】Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  4. 【leetcode刷题笔记】Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  5. Leetcode刷题C#版之 Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  6. Leetcode刷题C#版之 Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  7. Leetcode刷题C#版之Toeplitz Matrix

    题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the sam ...

  8. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  9. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

随机推荐

  1. java_线程的同步机制

    1.同步代码块: 使用synchronized包住可能会出现安全问题的代码 import static java.lang.Thread.sleep; class Test01{ public sta ...

  2. VINS-Mono论文笔记(未完)

    这是整篇论文的架构,下面针对每一部分进行自己的详细理解.(数学公式的问题没在博客里面解决,都是论文中的截图,尽可能美观==) 一.测量预处理部分(MEASUREMENT PREPROCESSING) ...

  3. Uva 12657 移动盒子(双向链表)

    题意: 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.可以执行以下4种指令:1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y右 ...

  4. Java面向对象学习-----类的成员变量2

    请定义一个交通工具(Vehicle)的类,其中有: 属性:速度(speed),体积(size)等等 方法:移动(move()),设置速度(setSpeed(int speed)),加速speedUp( ...

  5. 九度oj 题目1055:数组逆置

    题目1055:数组逆置 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8225 解决:3932 题目描述: 输入一个字符串,长度小于等于200,然后将数组逆置输出. 输入: 测试数据有多组 ...

  6. 【树状数组+离线查询】HDU 3333 Turing Tree

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000 ...

  7. 权限管理组件:rbac

    rbac: Role_Based Access Control,基于角色的权限控制 权限:一个包含正则表达式 的url就是一个权限 目录结构: rbac这个app中的文件代码如下: rbac/mode ...

  8. Linux下汇编语言学习笔记56 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. Balance POJ - 1837 地推

    Gigel has a strange "balance" and he wants to poise it. Actually, the device is different ...

  10. 选择器的使用(target选择器)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...