题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可)。

例如:传入参数为"the sky is blue   ",调整后的字符串为“blue is sky the”。

解题思路:先将字符串转换成字符数组的形式,然后对"the sky is blue   "整体做逆序调整,得到['e', 'u', 'l', 'b', ' ', 's', 'i', ' ', 'y', 'k', 's', ' ', 'e', 'h', 't'];然后再寻找每个单词边界,对每个单词做逆序的调整,最终连接成字符串即可。

def reverseWords(s):
"""单词间逆序
"""
s = s.strip(' ') # 去除首位空格
if s == None or len(s) == 0:
return None s = list(s)[::-1] # 寻找单词边界并作单词的逆序调整
start = -1
end = -1
for i in range(len(s)):
if s[i] != ' ':
start = i if (i==0 or s[i-1]==' ') else start
end = i if (i==len(s)-1 or s[i+1]==' ') else end if start != -1 and end != -1:
reverse(s, start, end)
start = -1
end = -1 return ''.join(s) def reverse(s, start, end):
"""单词逆序
"""
while start < end:
s[start], s[end] = s[end], s[start]
start += 1
end -= 1

LintCode翻转字符串问题 - python实现的更多相关文章

  1. lintcode :Reverse Words in a String 翻转字符串

    题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...

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

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

  3. leetcode python翻转字符串里的单词

    # Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: ...

  4. LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String

    公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: ...

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

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

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

  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. faiss学习

    faiss 学习 github wiki 介绍 Faiss:Facebook开源的相似性搜索类库 安装 在Mac系统编译安装Faiss faiss教程跟进--Makefile 编译 faiss安装 m ...

  2. [Django] Window上通过IIS发布Django网站

    网上的教程坑实在多,以下是本人亲测通过的: 需要解决的问题: 1.使用 python manage.py runserver 来运行服务器,只适用测试环境中使用,正式发布的服务,需要一个可以稳定而持续 ...

  3. [SoapUI] 检查测试步骤的类型或者或者某种特定类型的步骤列表

    SoapUI Groovy : Check if test step is of specific type, such as : Wsdl, Rest, Jdbc, HTTP, Groovy etc ...

  4. 一句话shell【php】

    1.mysql执行语句拿shell Create TABLE a (cmd text NOT NULL); Insert INTO a (cmd) VALUES('<?php @eval($_P ...

  5. 关于双重for循环封装数据问题

    1.问题描述 在使用双重for循环进行封装数据时出现一个问题,就是有的数据封装上了,有的数据未封装上,找了很久原因: for (A a:ListA) { for (B b: ListB) { if(a ...

  6. 1-JRE与JDK等知识

  7. ABP框架系列之三十二:(Logging-登录)

    Server Side(服务端) ASP.NET Boilerplate uses Castle Windsor's logging facility. It can work with differ ...

  8. log4j控制台乱码解决办法

    1.控制台编码问题 这两天碰到一个问题,在windows启动tomcat的时候tomcat控制台日志突然乱码了, 我发现在log4j.properties文件中加了一行配置log4j.appender ...

  9. JAVA 8 主要新特性 ----------------(四)Lambda函数式接口

    一.什么是函数式接口 只包含一个抽象方法的接口,称为函数式接口.  你可以通过 Lambda 表达式来创建该接口的对象.(若 Lambda 表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法 ...

  10. 计算机网络一:OSI七层、TCP/IP五层与TCP/IP四层

    一.OSI网络七层