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. POJ 1789Truck History(pirme)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22648   Accepted: 8781 De ...

  2. 全程图解 手把手教您开启windows终端服务

    一.什么是远程桌面? 远程桌面是微软公司为了方便网络管理员管理维护服务器而推出的一项服务.从windows 2000 server版本开始引入,网络管理员使用远程桌面连接程序连接到网络任意一台开启了远 ...

  3. Dedecms最新版本存储型XSS

    由于编辑文章的模板参数 typeid2可控,导致存储XSS发生. dedecms/dede/templets/article_edit.htm页面316-325行代码如下: <tr> &l ...

  4. mysql 索引 详解

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...

  5. 仿51job.com城市选择框特效

    650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...

  6. Java NIO原理和使用

    Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内 ...

  7. [bug]The file ‘/xxx/xxx.aspx’ has not been pre-compiled, and cannot be requested

    今天莫名奇妙的出现了这个问题,查找很多资料最后解决了. 发现编译的时候,少了一个dll,导致预编译失败. 参考资料 https://blogs.msdn.microsoft.com/asiatech/ ...

  8. Css transition

    CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并圆滑地以动画效果改变CSS的属性值. <!DOCTY ...

  9. linux 查找php.ini 文件

    sudo find /* -name 'php.ini' /etc/php5/fpm/php.ini

  10. C++编程思想重点笔记(上)

    C和C++指针的最重要的区别在于:C++是一种类型要求更强的语言.就void *而言,这一点表现得更加突出.C虽然不允许随便地把一个类型的指针指派给另一个类型,但允许通过void *来实现.例如: b ...