题目:

给定一个字符串,逐个翻转字符串中的每个单词。

样例

给出s = "the sky is blue",返回"blue is sky the"

说明

  • 单词的构成:无空格字母构成一个单词
  • 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括
  • 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个

解题:

这个题目方法很多的

1.整体反转,对每个单词再反转,但是中间的多个空格还有单独处理

2.把后面的单词,拿到前面去。参考程序

Java程序:

  1. public class Solution {
  2. /**
  3. * @param s : A string
  4. * @return : A string
  5. */
  6. public String reverseWords(String s) {
  7. // write your code
  8. if(s==null || s.length() == 0)
  9. return "";
  10. String[] array = s.split(" ");
  11. StringBuilder sb = new StringBuilder();
  12. for(int i = array.length - 1;i>=0 ;--i){
  13. if(!array[i].equals("")){
  14. sb.append(array[i]).append(" ");
  15. }
  16. }
  17. return sb.length()==0?"":sb.substring(0,sb.length() - 1);
  18. }
  19. }

总耗时: 1929 ms

网站今天增加好几道新题,然后我提交一次Pending。。。

Python程序:

  1. class Solution:
  2. # @param s : A string
  3. # @return : A string
  4. def reverseWords(self, s):
  5. # write your code here
  6. begin = 0
  7. end = 0
  8. while end< len(s):
  9. if s[end] == ' ':
  10. self.swap(s,begin,end - 1)
  11. begin = end + 1
  12. end = begin
  13. else:
  14. end += 1
  15. # print s
  16. self.swap(s,begin,end - 1)
  17. # print s
  18. self.swap(s,0,len(s) - 1)
  19. # print s
  20. return s
  21.  
  22. def swap(self,s,begin,end):
  23. while begin< end:
  24. tmp = s[begin]
  25. s[begin] = s[end]
  26. s[end] = tmp
  27. begin +=1
  28. end -=1

这个程序有点问题,没有解决的。。。

lintcode :Reverse Words in a String 翻转字符串的更多相关文章

  1. [LintCode] 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] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

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

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

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

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

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

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

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

  7. Leetcode151. Reverse Words in a String翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...

  8. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  9. Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度

    1.题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

随机推荐

  1. 如何在java类中读取Properties配置文件

    在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige  tea ...

  2. mvc涉及到input设置了disabled

    在做网站管理后台的用户修改功能时,由于当前用户修改个人信息时规定用户名不能修改,故使用了input标签的disabled属性,但是在提交数据后却发现用户名显示为空了.后来一查才知道input设置为di ...

  3. sublime text3的配置(整理)

    一.代码片段 开发人员很多时候是在做一些重复的工作. 针对不同数据表的增删改查都差不多,重复来重去的.很久不写程序了,利用十一假期在家看看书,写写程序. 最近一直很喜欢使用Sublime Text,发 ...

  4. easyui获取一行数据和修改data-options的值

    <table id="tab" class="easyui-datagrid" style="width: 100%; height: 500p ...

  5. hadoop自动安装的脚本与步骤

    最近要在10几台机器上安装hadoop.对于这种繁复而重复的工作,一步步的打命令行,对于程序员来说是一件不能忍的事情.所以我就琢磨着怎么写一个脚本来自动安装hadoop. 任务: 在10几台机器上中的 ...

  6. 原创的基于HTML/CSS/JavaScript的层级目录树

    之前参加过一些基于HTML/CSS/JavaScript的项目,当在页面中需要生成一颗目录树时,总是首先想着网上有没有现成的生成树的源代码,比如dtree.zthee,或者使用一些javascript ...

  7. Hadoop命令摘录

    一:文件操作 1.建立目录 [hadoop@hadoop1:hadoop]$bin/hadoop dfs -mkdir testdir 在HDFS中建立一个名为testdir的目录 2.上传文件到HD ...

  8. winrar 压缩文件方法

    问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...

  9. C#中DataTable与实体集合通用转换(使用扩展方法)

    本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...

  10. ThinkPHP学习笔记 实例化模型的四种方法

    创建Action类   [php]   <?php   class NewObjectAction extends Action{       public function index(){ ...