题目来源:

  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. C++中socket编程

    原文:http://blog.csdn.net/cuiran/article/details/5854794 Server端 #include <WINSOCK2.H> #include ...

  2. panel控件 换行

    Panel1.Controls.Add(new LiteralControl("<BR/>"));

  3. java之方法覆盖的坑

    昨天写了个小例子,覆盖hashCode.equals进行集合set的一些特性测试,代码如下: class Test3 { public int c; public Test3(int c) {this ...

  4. c++线程创建传递的参数发生改变

    看看如下代码,觉得输出会是什么? #include "stdafx.h" #include <windows.h> #include <iostream> ...

  5. jquery 实现横向滑动自动切换源码(同时显示多张图片)

    html代码: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  6. python----slots属性安全类

    问题:__slots__可以用于构造安全的类.如果一个类使用了__slots__那么它的属性就不在自由了. 下面举例说明: 1.自由属性. class person(object): def __in ...

  7. SQL Server 空间监测

    数据库文件型: select * from sys.dm_db_file_space_usage;      go                                           ...

  8. SQL Server 查看指定表上的索引

    解决方案: sys.indexs; ---------------------------------------------------------------------------------- ...

  9. jQuery插入节点的方法

    注:摘自<锋利的jQuery(第二版)> append() 向每个匹配的元素内部追加内容:  HTML代码:<p>我想说:</p>jQuery代码:$(" ...

  10. haproxy hdr_beg 配置

    v-dev-app01:/root# ping www.zjdev.com PING www.zjdev.com (192.168.32.16) 56(84) bytes of data. 64 by ...