【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相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
随机推荐
- MyEclipse2017修改Web Context Root
1,复制一个已经存在的项目,并修改项目名 2,选中项目右键选择properities,打开. 但是这里的web context root无法修改 3,删除web显示properties的所有属性,输入 ...
- BZOJ4832: [Lydsy1704月赛]抵制克苏恩 (记忆化搜索 + 概率DP)
题意:模拟克苏恩打奴隶战对对方英雄所造成的伤害 题解:因为昨(今)天才写过记忆化搜索 所以这个就是送经验了 1A还冲了个榜 但是我惊奇的发现我数组明明就比数据范围开小了啊??? #include &l ...
- 【原】简单shell练习(一)
1.交互式脚本 #!/bin/bash read -p "Enter your name:" name #read提示用户输入 echo "hello $name, we ...
- Python tldextract模块
最新发布的 PyPI: pip install tldextract 或者最新的开发版本: pip install -e 'git://github.com/john-kurkowski/tldext ...
- CF919F A Game With Numbers
题目:(luogu翻译错的很多) Alice和Bob玩游戏,每人有8张牌,牌的值为0~4.每一轮当前玩家选择自己的牌A和对手的牌B,然后将A的值变为( A + B )%5,其中A和B都不是0. 当一个 ...
- <Redis> 入门一 概念安装
Redis 概念 redis是一款高性能的NOSQL系列的非关系型数据库 什么是NOSQL NoSQL(NoSQL = Not Only SQL),意即“不仅 ...
- <c:foreach> 标签获取循环次数
<c:forEach var="i" begin="1" end="9" varStatus="status"&g ...
- Webbrowser 在web项目中的使用
string htmlstr = string.Empty; [STAThread] public string GetHtmlByWeb(string url) { try { RunWithSin ...
- [luoguP1040] 加分二叉树(DP)
传送门 区间DP水题 代码 #include <cstdio> #include <iostream> #define N 41 #define max(x, y) ((x) ...
- 前端开发:HTML
静态页面: 没有与用户进行交互,而仅仅是用户浏览的一个网页 动态网页:就是用户不仅仅可以浏览网页,还可以与服务器交互 Web前端应用场景:公司官网(在PC通过浏览器访问公司网站).移动端网页(在手机上 ...