题目

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

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

click to show clarification.

Clarification:

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

题解

一个用了java的s.split(" "),按空格分隔。

然后调用了系统函数:Collections.reverse(list);把list顺序调换了。

最后再把结果存成数组即可。

代码如下:

 1    public static String reverseWords(String s) {
 2         if(s==null||s.length()==0)
 3             return s;
 4         String [] result = s.split(" ");
 5         if(result==null||result.length==0)
 6             return "";
 7             
 8         ArrayList<String> list = new ArrayList<String>();
 9         
         for(int i = 0; i<result.length;i++){
             if(!result[i].isEmpty())
                 list.add(result[i]);
         }
         Collections.reverse(list);
         
         String ans = new String();
         for(int i = 0; i<list.size()-1;i++){
             ans += list.get(i)+" ";
         }
         ans +=list.get(list.size()-1);
         return ans;
     }

Reverse Words in a String leetcode java的更多相关文章

  1. 151. Reverse Words in a String(java 注意细节处理)

    题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...

  2. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  3. LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)

    这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...

  4. Reverse Words in a String——LeetCode

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

  5. Reverse Words in a String leetcode

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

  6. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  7. Interleaving String leetcode java

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...

  8. Reverse Words in a String | LeetCode OJ | C++

    我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...

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

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

随机推荐

  1. 手把手教你做爬虫---基于NodeJs

    前言: 趁着北京今儿天气格外的蓝,我觉得我得干点什么,于是乎,卷起袖子,整理一下最近做爬虫的那些事儿. 目标:爬取北京大学软件与微电子学院的所有新闻,并将内容及图片存储到本地. 设计思路:经过对北京大 ...

  2. android 地图

    ========= Y:\AMap_Android_API_3DMap_Demo\android_studio\AMap3DDemo>keytool -v -list -keystore C:\ ...

  3. (android高仿系列)今日头条 --新闻阅读器 (转载)

    非常不错,原文地址:http://blog.csdn.net/vipzjyno1/article/details/26514543

  4. VM 操作系统实例化(基于 KVM 的虚拟化研究及应用--崔泽永(2011))的论文笔记

    一.VM操作系统实例化 1.建立虚拟磁盘镜像 虚拟磁盘镜像在逻辑上是提供给虚拟机使用的硬盘, 在物理上可以是 L inux系 统内一普通镜像文件, 也可以是真实的物理磁盘或分区. 本方案设计中将虚拟机 ...

  5. 20172302『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结

    一.结对对象 姓名:周亚杰 学号:20172302 担任角色:驾驶员(周亚杰) 伙伴第二周博客地址 二.本周内容 (一)继续编写上周未完成代码 1.本周继续编写代码,使代码支持分数类计算 2.相关过程 ...

  6. hdoj-2066-一个人的旅行(迪杰斯特拉)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. Versaloon -- Connect To Targets

    Versaloon Full open-source(GPLv3) platform for multiple applications, including programmer, debugger ...

  8. NSZombie 详解 -僵尸对象

    1.什么是僵尸对象? 简而言之,就是过度释放的对象. 2.僵尸对象有什么特点? 如果一个对象a被变成了僵尸对象,那么,在进行下面的判断时,a是会被系统当成一个对象来进行判断的.但是,如果你使用a进行其 ...

  9. 查看Linux进程CPU过高具体的线程堆栈(不中断程序)

    转自:http://blog.csdn.net/mergerly/article/details/47731305 1.TOP命令,找到占用CPU最高的进程 $ top top - 20:11:45  ...

  10. Quartz的集群模式和单机模式共存-让一个非集群的Quartz与集群节点并行着运行

    假如你让一个非集群的 Quartz 应用与集群节点并行着运行,设法使用 JobInitializationPlugin和 RAMJobStore Quartz支持可选节点执行jobquartz集群,会 ...