题目描述:试实现一个函数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. Polynomial regression

  2. 创建第一次C语言程序

    在这里我以VS2015为例,做演示.为什么要去演示怎样创建项目尼,因为我写第一个程序时,不知道该怎样用VS创建我的第一个应用程序. 第一步:打开VS环境如下 第二步:在开始出点击“新建项目”或在右上角 ...

  3. input text 只能输入数字

    添加 onkeyup="value=value.replace(/[^\d]/g,'')"

  4. 244. Shortest Word Distance II 实现数组中的最短距离单词

    [抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...

  5. 【转载】一个小时学会MySQL数据库

    一个小时学会MySQL数据库   目录 一.数据库概要 1.1.发展历史 1.1.1.人工处理阶段 1.1.2.文件系统 1.1.3.数据库管理系统 1.2.常见数据库技术品牌.服务与架构 1.3.数 ...

  6. 安装配置python环境,并跑一个推荐系统的例子

    1.官网下载python2.7,安装完后,在环境变量Path中加上这个路径 在控制台输入python,出现版本信息,就成功了. 2.我使用的是 pycharm,注册后,在 把自己的python.exe ...

  7. Python:每日一题006

    题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.…… 个人思路及代码: # 方 ...

  8. .net core 中使用ef 访问mysql

    1.参考文档说修改项目文件添加,就得这么做,不然会报错 <ItemGroup> <DotNetCliToolReference Include="Microsoft.Ent ...

  9. Spring 添加属性集中常见方法

    //创建容器,索要对象, package cn.lijun.Test; import org.junit.Test;import org.springframework.context.Applica ...

  10. session和cookie相关知识总结

    HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有必要纪录彼此过去的行为,每一次请求之间都是独立的. 人们很快发现 ...