【LeetCode刷题Java版】Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
- 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的更多相关文章
- 【leetcode刷题笔记】Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- 【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 ...
- 【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-> ...
- Leetcode刷题C#版之 Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- 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 ...
- Leetcode刷题C#版之Toeplitz Matrix
题目: Toeplitz Matrix A matrix is Toeplitz if every diagonal from top-left to bottom-right has the sam ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
随机推荐
- 了解Java密码扩展的基础
了解Java密码扩展的基础 Java密码扩展(The Java Cryptography Extension),是JDK1.4的一个重要部分,基本上,他是由一些包构成的,这些包形成了一个框 ...
- 09CSS高级定位
CSS高级定位 定位方式——position position:static|absolute|relative static表示为静态定位,是默认设置. absolute表示绝对定位,与下位置属 ...
- [驱动] 一个简单内核驱动,通过qemu调试(1)
模块 通过在HOST上修改linux kernel源代码,重新编译一个vmlinux,然后,通过qemu根据这个bzImage 启动一个vm,进行调试 #cat drivers/char/test.c ...
- Getting start with dbus in systemd (02) - How to create a private dbus-daemon
Getting start with dbus in systemd (02) 创建一个私有的dbus-daemon (session) 环境 这里我们会有两个app: app1(client),ap ...
- 面试之JVM
目录 Java虚拟机 类的加载方式 Java内存模型 程序计数器(PC) Java虚拟机栈(Stack) 本地方法栈 元空间(MetaSpace) Java堆(Heap) 内存分配策略 垃圾回收(GC ...
- iOS缓存到内存
前面一片文章介绍了如何上传和下载文件,这篇文章将介绍一下如何在iOS设备中进行缓存. 这篇文章将只介绍一下将内容缓存到内存中,下一篇文章就介绍一下在iOS磁盘上缓存内容. 使用缓存的目的是为了使用的应 ...
- 诊断:ORA-00376 & ORA-01110
现象: Errors in file /path/of/diag/rdbms/prod/PROD/trace/PROD_ora_13447.trc: ORA-00376: 此时无法读取文件 61 OR ...
- glibc库函数,系统调用API
glibc封装了大部分系统API,我们一般都是使用glibc封装的接口进行系统调用,碰到一些没有封装的接口,可以通过这个 函数syscall 进行系统调用. /* Invoke `system c ...
- windows下mysql 5.7版本中修改编码为utf-8的方法
方法如下 首先通过 show variables like 'character_set_%';查看mysql字符集情 默认编码为 latin1 然后关闭数据库 在mysql安装目录下找到my.ini ...
- 86-Money Flow Index 资金流量指数指标.(2015.7.3)
Money Flow Index 资金流量指数指标 计算: 1.典型价格(TP)=当日最高价.最低价与收盘价的算术平均值 2.货币流量(MF)=典型价格(TP)×N日内成交金额 3.如果当日MF> ...