LeetCodeOJ--Reverse Words in a String(python版本)
来源:互联网 发布:淘宝里卖家正在被处罚 编辑:程序博客网 时间:2024/06/03 20:08
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
click to show clarification.
Clarification:
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
1.测试版本代码:
# -*- coding: cp936 -*-s = " tian zhai xing "print "Before s:",sdef reverseWords(s): L = s.split() #单词拆分成列表 print "字符串以单词分割成列表: %s" % L L.reverse()#反转列表中单词 print "反转列表中的单词: %s" % L s1 = ' '.join(L)#列表以空格分割单词,返回字符串 print "以空格分隔列表中反转后的单词: %s" % s1 return s1 #return s1.rstrip()#字符串删除首尾空格和回车L = reverseWords(s)print "Afer L :",L
2.测试版本截图:
3.提交AC版本代码:
class Solution: # @param s, a string # @return a string def reverseWords(self, s): L = s.split()#字符串以单词分隔成列表 L.reverse()#反转列表中的单词 s1 = ' '.join(L)#以空格分隔列表中反转后的单词,返回字符串 return s1
0 0
- LeetCodeOJ--Reverse Words in a String(python版本)
- LeetCodeOJ--Reverse Words in a String
- leetcodeOJ 557. Reverse Words in a String III
- Leetcode - Python - Reverse Words in a String
- Reverse words in a string.
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- 谈一谈汽车无法行驶应该怎么办
- 谷歌地图在ArcGIS中如何将默卡托转成WGS84坐标系
- ognl.OgnlException
- 表单验证3
- 程序的模块化的一些见解5-移植性-实例说明
- LeetCodeOJ--Reverse Words in a String(python版本)
- HDOJ-1213(并查集)
- cg
- 代码就是把当前节点内所有进程遍历一遍,把进程状态写入到文本里面
- checkStyle配置说明、范例和结果分析
- bash: wget: command not found的解决办法
- 无限层级且乱序的树形结构数据的整理,利用HashMap减少遍历次数
- 字符串化操作符#,粘接附##,字符化操作符@#
- 【转】 LSD(Line Segment Detector) 直线段检测算法学习~