问题描述:

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

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

解题思路:

先利用split()方法将句子按空格分为几个单词的列表,

然后reverse()函数使列表逆序,

最后join函数以空格为间隔拼成字符串返回结果。

Python代码:

class Solution:
    # @param s, a string
    # @return a string
    def reverseWords(self, s):
        a = s.split()
        a.reverse()
        return ' '.join(a)

LeetCode OJ1: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] 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 ...

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

  4. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  5. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

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

  7. leetcode - [1]Reverse Words in a String

    Question: Reverse Words in a String Given an input string, reverse the string word by word. For exam ...

  8. LeetCode 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. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

随机推荐

  1. HTML5之文件API

    问题很简单,做个上传文件的页面. <!-- multiple代表可上传多个文件 --> <input type="file" id="file" ...

  2. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. MVC Html.BeginForm 与 Ajax.BeginForm 使用总结

    最近采用一边工作一边学习的方式使用MVC5+EF6做一个Demo项目, 期间遇到不少问题, 一直处于研究状态, 没能来得及记录. 今天项目进度告一段落, 得以有空记录学习中遇到的一些问题. 由于MVC ...

  4. Euler猜想

    这是从http://duodaa.com/blog/index.php/archives/538/截得图,以下是代码 package math; import java.math.BigDecimal ...

  5. 从业十余年谈谈对dotnet看法与坚持

    前言 园子经常在讨论关于.Net发展的问题,我也这些年在工作乃至创业过程中使用.Net碰到的一些问题和看法.个人擅长的技术面,C/C++/MFC/STL.ASM.JAVA.VB.Javascript/ ...

  6. PHP多级联动的学习(二)

    首先我发现实现点击下拉框中的选项跳转传递信息的功能是需要javascript实现的.于是我把相应代码拷过来,把跳转的地址改掉.接着我发现无法 把<option value=''>中valu ...

  7. eclipse下创建maven项目

    1.创建一个Java项目 1)File--->New--->Other--->Maven--->Maven Projet 2)选择maven-archetype-quickst ...

  8. Files 的值“<<<<<<< .mine”无效。路径中具有非法字符

    解决冲突,告诉SVN这个问题已解决(Resolved). 一般更简单些:在你的工程OBJ/DEBUG目录下,找到 工程名.csproj.FileListAbsolute.txt的文件打开并删除含有'& ...

  9. 设置Flush刷新模式setFlushMode()

    参考 http://blog.csdn.net/superdog007/article/details/38852399 FlushMode的枚举值: FlushMode.ALWAYS:任务一条SQL ...

  10. Python之路【第一篇】python基础

    一.python开发 1.开发: 1)高级语言:python .Java .PHP. C#  Go ruby  c++  ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...