LintCode翻转字符串问题 - python实现
题目描述:试实现一个函数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实现的更多相关文章
- lintcode :Reverse Words in a String 翻转字符串
题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- leetcode python翻转字符串里的单词
# Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: ...
- LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 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& ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- 二叉搜索树与双向链表(python)
题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. # -*- coding:utf-8 -*- # class TreeNo ...
- 100-days: twelve
Title: Mark Zuckerberg wants Facebook's to emulate China's WeChat, but can it? emulate v.效仿,模仿 As ...
- 你确定你真的懂Nginx与PHP的交互?
Nginx是俄国人最早开发的Webserver,现在已经风靡全球,相信大家并不陌生.PHP也通过二十多年的发展来到了7系列版本,更加关注性能.这对搭档在最近这些年,叱咤风云,基本上LNMP成了当下的标 ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- stc15w wave
1. 定时器和延时 #include "15W4KxxS4.h" #define FOSC 12000000 #define CLK (65536-FOSC/2/12/1000) ...
- Integer 与int的区别
1.在的model的时候很多喜欢用int 类型 但是最好用Integer类型因为在查询的时候如果返回不到数据 Model就会报这个类是空的 所以应该尽量选用interger
- 13. The Impact of New Technology on Business 新科技对商务的影响
13. The Impact of New Technology on Business 新科技对商务的影响 (1) New technology links the world as never b ...
- 部署eclipse项目到tomcat
1.为了以防万一,将本地tomcat版本及其jdk版本与服务器上的版本最好是相同的 2.在本地eclipse下运行项目即可发布(注意(1)数据库连接的是服务器数据库还是本地数据库(2)运行项目前先cl ...
- 2018年2月19日我的java学习
2019/2/18 星期一今天学习了Java 中的面向对象思想主要学习了类 构造器等在学习修饰属性的过程中,有4点必须牢记前提是理解类的各种关系 类中有5种关系 本身 同包类 同包继承子类 不同包继承 ...