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

Example:

Input: "the sky is blue",
Output: "blue is sky the".
Note:

A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up: For C programmers, try to solve it in-place in O(1) space.

思路

1.  split by a single or multiple spaces, making sure remove leading or trailing spaces

2. to reverse,  traverse the input from right to left,  append each item into StringBuilder

代码

 public class Solution {
public String reverseWords(String s) {
// corner case
if(s == null || s.length() == 0) return "";
// s.trim() 去除leading or trailing spaces
// s.split("\\s+")根据a single or multiple spaces 来split字符串
String[] words = s.trim().split("\\s+");
// use StringBuilder to save result
StringBuilder sb = new StringBuilder();
// to reverse, from right to left
for(int i = words.length-1; i >= 0; i--){
sb.append(words[i]+" ");
}
// 确保结果中去除了leading or trailing spaces
return sb.toString().trim();
}
}

[leetcode]151. Reverse Words in a String翻转给定字符串中的单词的更多相关文章

  1. LeetCode 557. Reverse Words in a String III (反转字符串中的单词 III)

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  2. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

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

  3. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  4. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  5. LeetCode 151 reverse word in a string

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

  6. Java for LeetCode 151 Reverse Words in a String

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

  7. leetcode 151. Reverse Words in a String --------- java

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

  8. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

  9. Leetcode#151 Reverse Words in a String

    原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...

随机推荐

  1. SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)

    一.导包: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  2. open read split

    open  来打开文件, 其具体表现为 open('文件名或路径', 'r or w or other', 位置?) 其生成一个文件类型的对象 file object. 可写做 FILENAME = ...

  3. ACM__菜鸟之经典错误

    1:多组输入与单组输入 Input contains multiple test cases. Input contains a single test case. 2:  EOF=-1 while( ...

  4. ReactiveX 学习笔记(17)使用 RxSwift + Alamofire 调用 REST API

    JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...

  5. 使用rgba设置输入框背景透明

    项目中遇到要求输入框的背景设置透明度,但文字不受影响,如下图 输入框使用input标签 <input ref="searchText" type="search&q ...

  6. MySQL查询提示

    MySQL查询提示: 1.LOW_PROPRITY,HIGHT_PRIORITY 作用:指定sql语句的运行优先级,会将加了HIGHT_PROPRITY提示的sql调度到表访问队列的最前面 限制:仅对 ...

  7. 收藏点webservice接口

    商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebSe ...

  8. eclipse git 创建新分支 合并分支 删除分支

    创建分支: 合并分支: 删除分支:

  9. 利用ant脚本 自动构建svn增量/全量 系统程序升级包【转】

    引文:我们公司是做自己使用产品,迭代更新周期短,每次都花费较多时间和精力打包做增量更新,发现了一篇文章用于 自动构建svn增量/全量 系统程序升级包,收藏之,希望可以通过学习,更加简化我们的工作. 文 ...

  10. you-get

    1.打开cmd,输入命令并执行 pip3 install you-get 2.输入命令,检测 You-Get 是否安装成功 you-get 3.开始下载吧 you-get [视频地址]you-get ...