『Python学习』lettcode题目答案python版
2016-05-26 14:52
344 查看
注:本文部分参考以下文章
LeetCode题解整理版(二)
1、将abc def形式的字符串翻转成def abc,并且去掉多余的空格
2、判断字符串 C 是不是由字符串 A 和字符串 B 组成
3、求一个集合的所有子集
4、
LeetCode题解整理版(二)
1、将abc def形式的字符串翻转成def abc,并且去掉多余的空格
class Solution: def reverseWords(self, s): return "".join([word[::-1] for word in s[::-1].split()]) solution = Solution() print solution.reverseWords("abc def")
2、判断字符串 C 是不是由字符串 A 和字符串 B 组成
class Solution: """ s1, s2, s3 均是字符串 思路一是如果 len(s3) != len(s1) + len(s2) 则 s3 不是由 s1 和 s2 组成的 思路二是先把 s1, s2, s3 转变为列表, 然后从 s3 列表中把 s1, s2 列表中有的元素都移除掉,如果最后 s3 列表为空则 s3 是由 s1 和 s2 组成的, 否则则不是。同时移除的过程中报 ValueError 的话, 也说明不是 """ def isInterleave(self, s1, s2, s3): if len(s3) != len(s1) + len(s2): print "%s不是由%s和%s组成的" %(s3, s1, s2) return s4 = list(s3) try: for s1_i in list(s1): s4.remove(s1_i) for s2_i in list(s2): s4.remove(s2_i) except ValueError: print "%s不是由%s和%s组成的" %(s3, s1, s2) return else: print "%s是由%s和%s组成的" %(s3, s1, s2) if s4: print "%s不是由%s和%s组成的" %(s3, s1, s2) solution = Solution() solution.isInterleave("abc", "def", "abcdeg")
3、求一个集合的所有子集
class Solution(): """ s 是一个集合 """ def subset(self, s): subsetlist = [[], s] for i in range(len(s)): for j in range(i+1, len(s)+1): subsetlist.append(s[i:j]) solution = Solution() solution.subset([1, 2, 3])
4、
相关文章推荐
- python 后台运行
- python Flask restful框架
- 中文python笔记
- python【5】-生成式,生成器
- python的文件操作
- Python微博地点签到大数据实战 微博API的使用
- 【数据挖掘】利用md5查找重复文件
- OpenStack python库: from oslo.config import cfg ImportError: No module named config
- Python 基础语法
- python基础(1)
- python【4】-函数
- 70. Climbing Stairs [easy] (Python)
- Python模块探秘 Smtplib发送带有各种附件的邮件
- Python模块探秘 Smtplib发送带有各种附件的邮件
- 83. Remove Duplicates from Sorted List [easy] (Python)
- 整理--python知识点
- python开发环境管理:pip和virtualenv
- 342. Power of Four [easy] (Python)
- Python中docstring文档的写法
- 使用python开发json、csv数据格式转换工具