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.
  1. package com.liuhao.acm.leetcode;
  2.  
  3. /**
  4. * @author liuhao
  5. *
  6. * Given an input string, reverse the string word by word. For example,
  7. * Given s = "the sky is blue", return "blue is sky the".
  8. */
  9. public class ReverseWords {
  10.  
  11. public static String reverseWords(String s) {
  12.  
  13. // 若输入字符串直接是空串。则直接返回该字符串
  14. if (s.equals("")) {
  15. return "";
  16. }
  17.  
  18. // 将字符串按空格"\\s{1,}"进行切割,一个或者多个空格的正则:"\\s{1,}"
  19. String[] strArr = s.split("\\s{1,}");
  20. int len = strArr.length;
  21.  
  22. // 切割后字符串变成空串,直接返回
  23. if (len == 0) {
  24. return "";
  25. }
  26.  
  27. // 用StringBuilder更加有效
  28. StringBuilder sb = new StringBuilder("");
  29.  
  30. // 反向输出之前切割的字符串数组
  31. for (int i = len - 1; i >= 0; i--) {
  32. if (!strArr[i].equals("")) {
  33. sb.append(strArr[i]);
  34. sb.append(" ");
  35. }
  36. }
  37.  
  38. sb.deleteCharAt(sb.lastIndexOf(" "));
  39.  
  40. return sb.toString();
  41. }
  42.  
  43. public static void main(String[] args) {
  44. System.out.println(reverseWords(""));
  45. }
  46.  
  47. }

【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. caffe实现自己的层

    http://blog.csdn.net/xizero00/article/details/52529341 将这篇博客所讲进行了实现 1.LayerParameter也在caffe.proto文件中 ...

  2. apt-get update 报错 W: Unknown Multi-Arch type 'no' for package 'compiz-core'

    源 #deb包 deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http:// ...

  3. java去左右的空格(包括全角空格,tab,回车等)

    在开发中我们会遇到需要去除左右空格的需求,如果只是简单的空格,调一下trim()方法即可,但如果有中文全角.回车等看起来是空格的非空格,则需要自定义来开发实现,下面这个工具可以实现去左右那些看起来是空 ...

  4. hdu 1792 A New Change Problem(互质数之间最大不能组合数和不能组合数的个数)

    题意:求互质的m和n的最大不能组合数和不能组合数的个数 思路:m和n的最大不能组合数为m*n-m-n,不能组合数的个数为(m-1)*(n-1)/2 推导: 先讨论最大不能组合数 因为gcd(m,n)= ...

  5. Python学习-初始列表

    初始列表 列表中的排放是没有顺序的并且可以被修改 在列表中你可以放入任何的元素类型.字符串,数字,布尔值等等,甚至还可以进行列表的嵌套 列表的注意特征:用中括号包围,中间用逗号分隔开 list = [ ...

  6. Format 格式化函数

    转自:老百姓 Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明:function Format(const ...

  7. Matlab学习笔记(三)

    二.MATLAB基础知识 (四)数组 MATLAB总是把数组看作存储和运算的基本单位,标量数据也被看作是(1×1)的数组 一维数组的创建 创建一维数组的几种方法:(e_two_14.m) 直接输入法: ...

  8. 79-Envelopes,包络指标.(2015.7.1)

    Envelopes 包络指标 观井映天 2015.7.1

  9. C51 独立按键 个人笔记

    独立按键类似于一个开关,按下时开关闭合 防抖 硬件防抖 软件防抖 通过延时,滤掉抖动的部分 电路图 普中科技的开发板,独立按键电路图如下 判断按键按下 因此判断是否按下开关的方法是看引脚是否为低电平( ...

  10. Java AOP

    AOP 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之.翻译过来 ...