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. PHP 之pthreads多线程模块在windows下的安装

    一.查看phpinfo 二.下载pthreads扩展 下载地址:http://windows.php.net/downloads/pecl/releases/pthreads/ 三.复制文件 复制ph ...

  2. 安装exe4j出现jre不匹配问题

    在安装exe4j 客户端,提示如下错误: 提示的错误信息大意如下:install4j安装时,在本系统中没有找到JRE(JavaRuntime Environment)(版本要求:最低1.5,最高1.6 ...

  3. Python使用Flask框架,结合Highchart,自定义导出菜单项目及顺序

    参考链接: https://www.highcharts.com.cn/docs/export-module-overview https://api.hcharts.cn/highcharts#ex ...

  4. nodejs学习(一) ---- nodejs + express应用生成器 快速创建应用

    1.node安装及环境配置(自行百度) 2.express安装及配置 (自行百度) 3.通过应用生成器工具 express 快速创建应用骨架   全局安装应用生成器 : npm install exp ...

  5. tcpdump用于抓取tcp数据包

    一.简单使用:-c监听次数.-v打印详情.host后接监听地址 1.1.监听 tcpdump -c -v host www.baidu.com 1.2.访问被监听的网址: 1.3.查看监听的数据:

  6. LAMP中添加多虚拟主机

    在/etc/apache2/sites-available中默认有个default文件,其中的大致配置如下: <VirtualHost *:80> ServerAdmin xujie198 ...

  7. prop 和 attr 中一些羞羞的事情

    引言 前几天做一个迷你京东小项目的时候涉及到一个全选的小功能,一开始用的是 attr,但是效果完全不是自己想要的,当商品按钮点击过一次后,attr就无法对其状态进行更改,最后谷歌了一番发现需要用 pr ...

  8. Variational Auto-Encoders原理

    目录 AE v.s. VAE Generative model VAE v.s. GAN AE v.s. VAE Generative model VAE v.s. GAN

  9. AspNetPager控件的简单使用

    1.首先引用这个控件. 在前台页面上修改一些属性. <webdiyer:AspNetPager ID=" AlwaysShow="true" FirstPageTe ...

  10. C51 定时器/计数器 个人笔记

    C51的周期 结构图 两个功能寄存器 51单片机定时/计数器的工作由两个特殊功能寄存器控制.TMOD用于设置其工作方式:TCON用于控制其启动和中断申请. 工作方式寄存器TMOD 其中方式一和方式二常 ...