leetcode 151 Reverse Words in a String (python)
2016-07-18 18:33
513 查看
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",
return "blue is sky the".
python代码:
(完)
题目描述:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
python代码:
class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ rs=s[::-1] #将整个字符串反转 l=rs.split() #将反转后的字符串通过split()函数进行分割,产生的单词发在列表l中 ls=[word[::-1] for word in l] #使用列表解析,反转列表l中的每一个单词 return ' '.join(ls) #使用join()函数,将列表ls中的单词,通过空格连接成一个字符串
(完)
相关文章推荐
- 以写代学:python实现 剪刀、石头、布游戏
- Python处理txt数据实例
- python命令行下tab键补全命令
- python变量
- python对文件的读取
- Python 小甲鱼教程 乌龟吃鱼游戏
- python正则表达式--基本用法和函数(简单实例)
- Python爬虫实战(4):豆瓣小组话题数据采集—动态网页
- python练习
- python入门——猜数字游戏
- 解决sublime Text编译python中文脚本出错
- 我的python学习--第三天
- 老司机带你用vagrant打造一站式python开发测试环境
- python魔法方法-单目运算及一般算数运算
- python中sys.exit() os._exit() exit() quit()的简单使用
- mpi学习日志(1):mpi与python
- Python的__getattr__和__getattribute__
- 习题11 提问
- Python里的string 和 unicode
- python正则表达式--基本用法和函数(理论知识)