题目来源:

  https://leetcode.com/problems/reverse-words-in-a-string/


题意分析:

  给定一个字符串,里面包括多个单词,将这个字符串的单词翻转,例如"the sky is blue",得到"blue is sky the".


题目思路:

  首先获取每个单词,将单词记录到一个数组里面,然后翻转过来就行了。要处理的是前面和后面有空格的情况。


代码(python):

class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) == 0:
return ""
ans = []
begin = i = 0
mark = True
while i < len(s):
if s[i] != ' 'and mark:
begin = i
mark = False
if s[i] == ' ' and i > 0 and s[i - 1] != ' ':
ans.append(s[begin:i])
while i < len(s) and s[i] == ' ':
i += 1
begin = i
i += 1
if s[-1] != ' ':
ans.append(s[begin:len(s)])
#print(ans)
j = len(ans)
if j == 0:
return ""
res = ans[j - 1]
j -= 1
while j > 0:
res += ' ' + ans[j - 1]
j -= 1
return res

  

[LeetCode]题解(python):151-Reverse Words in a String的更多相关文章

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

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

  3. 【刷题-LeetCode】151 Reverse Words in a String

    Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...

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

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

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

  6. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  7. Java for 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 ...

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

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

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

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

随机推荐

  1. JQuery获取Checkbox组的值

    前台: <div id="addtrtr" style="padding:20px; background-color:#F8F8F8;"> < ...

  2. input file 样式以及获取选择文件方法集合

    样式一(http://www.cnblogs.com/jason-liu-blogs/archive/2013/06/13/3133377.html) <style> a{display: ...

  3. web.config详解

    在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代 ...

  4. HTML7常用的类型刮刮乐 光棒效果

    常用的类型: 1.数学: Math.ceil():天花板数 Math.floor():地板数 Math.round():四舍五入取整数 Math.random():生成0-1之间的随机数   2.日期 ...

  5. JS参数使用带参数的方法

    大家都知道,在JS之中,一个变量的生命周期不是以大括号为界限的,所以即使是使用在循环或判断中的变量,外部也可以使用.可如果我们在循环或变量中使用了方法,而且这个方法使用了循环中的变量,那么后面的代码是 ...

  6. Struts学习之ValueStack学习

    1. 数据传输背后机制:ValueStack(值栈) 在这一切的背后,是因为有了ValueStack(值栈)! ValueStack基础:OGNL OGNL是Struts2中使用的一种表达式语言,它可 ...

  7. AOP-----动态代理(转)

    动态代理是实现AOP的绝好底层技术 JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Proxy和InvocationHandler.其中 InvocationHandler是 ...

  8. thinkphp从数据库里的html代码显示页面不解析

    首先,这个问题不应该出现在这里,因为以前在用ThinkPHP3.1.2的时候,利用富文本编辑器保存文本后,直接从数据库里面取出的数据都能正常显示,改用ThinkPHP3.2.3之后,thinkphp从 ...

  9. RSA—非对称加密算法

    RSA:非对称加密算法加解密原理如下:已知:p,q,n,e,d,m,c其中:p与q互为大质数,n=p*q 公钥Pk(n,e):加密使用,是公开的 私钥Sk(n,d):解密使用,不公开 c:明文 m:密 ...

  10. NetworkManager——Linux强大的网络管理工具

    NetworkManager服务:NetworkManager - Linux Networking made Easy NetworkManager简介:NetworkManager由一个管理系统网 ...