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

For example,
Given s = "the sky is blue",
return "blue is sky the".

解题思路:

本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下:

    public String reverseWords(String s) {
if (s == null || s.length() == 0)
return s;
String[] str = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = str.length - 1; i >= 0; i--)
if (str[i].length() >= 1)
sb.append(str[i] + " ");
if (sb.length() > 1)
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}

Java for LeetCode 151 Reverse Words in a String的更多相关文章

  1. [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& ...

  2. 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& ...

  3. [leetcode]151. Reverse Words in a String翻转给定字符串中的单词

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

  4. 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 ...

  5. Leetcode#151 Reverse Words in a String

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

  6. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  7. [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 ...

  8. Java for LeetCode 092 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-> ...

  9. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

随机推荐

  1. 使用ssis完成excel的数据导入

    SSIS(SQL Server Integration Service)是从MS SQL 2005开始引入的,是一种ETL(Extract Transform Load)工具,SSIS比普通的ETL更 ...

  2. 43.Android之ListView中BaseAdapter学习

    实际开发中个人觉得用的比较多是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView.GridView.Gallery.Spinner ...

  3. BZOJ-1050 旅行comf 并查集+乱搞

    好久以前codevs上做过的,拿着改了改.. 1050: [HAOI2006]旅行comf Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2194 S ...

  4. javaScript基础练习题-下拉框制作(JQuery)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 洛谷P2507 [SCOI2008]配对

    题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对. ...

  6. git使用记录

    唔,git有本地版本管理功能,所以,这个完全是可以拿来自己做版本管理的.所以有必要学习一下,另外,在oschina上开了个账户,用来管理自己一些代码,也是增加自己学习git的动力. 1. 使用clon ...

  7. css常用技巧

    去点号 list-style:none; 字体居中 text-align:center; 链接去下划线 text-decoration:none; 鼠标禁止右键 <body oncontextm ...

  8. C/C+小记

    1.struct与typedef struct struct Student{int a;int b}stu1; //定义名为Student的结构体,及一个Student变量stu1 struct { ...

  9. go runtime scheduler

     http://www.slideshare.net/matthewrdale/demystifying-the-go-scheduler http://www.cs.columbia.edu/~a ...

  10. zabbix 邮件报错 Support for SMTP authentication was not compiled in

    服务器系统是centos6.5 zabbix版本是3.0.4 根据 网上教程配置好邮件脚本后,触发发送邮件的时候报错: Support for SMTP authentication was not ...