LintCode python 小白-简单题-22平面列表
2017-07-29 18:00
465 查看
题目:给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。
注意事项:如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。
样例:
给定 [1,2,[1,2]],返回 [1,2,1,2]。
给定 [4,[3,[2,[1]]]],返回 [4,3,2,1]。
思路:递归算法,对列表进行列表的查找。。。。
相关知识:
type的运用,以后在数字的判断也会更加方便。
http://blog.csdn.net/jcjc918/article/details/9623109
本次用到了类的知识,特别在self.b
代码:
注意事项:如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。
样例:
给定 [1,2,[1,2]],返回 [1,2,1,2]。
给定 [4,[3,[2,[1]]]],返回 [4,3,2,1]。
思路:递归算法,对列表进行列表的查找。。。。
相关知识:
type的运用,以后在数字的判断也会更加方便。
http://blog.csdn.net/jcjc918/article/details/9623109
本次用到了类的知识,特别在self.b
代码:
class Solution(object): # @param nestedList a list, each element in the list # can be a list or integer, for example [1,2,[1,2]] # @return {int[]} a list of integer def flatten(self, nestedList): # Write your code here a=nestedList if a is None: #判断是否为空 return [] self.b=[] if type(a)!=list:#type()进行类型的判断 self.b.append(a) else: self.digui(a) return self.b def digui(self,c): if c is None: #判断是否是[]情况 retur for d in c: if type(d) == list: #发现列表内存在列表 self.digui(d) else: #不是列表代表是数字,将数字放于self.b的后面 self.b.append(d)
相关文章推荐
- LintCode python 小白-简单题-548两数组的交Ⅱ
- LintCode python 小白-简单题-457经典二分查找问题
- LintCode python 小白-简单题-2 尾部的0
- LintCode之22 平面列表
- LintCode python 小白-简单题-445余弦相识度
- LintCode python 小白-简单题-646 First Position Unique Character
- LintCode python 小白-简单题-627最长回文串
- LintCode python 小白-简单题-569各位相加
- LintCode python 小白-简单题-413-反转整数
- LintCode python 小白-硬币-博弈论,动态规划
- LintCode python 小白5-丑数
- LintCode python 小白3-三角形计数
- LintCode python 小白7-超级丑数
- LintCode python 小白4-Big Integer Addition
- LintCode python 小白6-丑数 II
- LintCode 【平面列表】
- Lintcode:平面列表
- Python开发入门与实战22-简单消息回复
- python 列表,元组,字典,集合简单总结
- 小白学Python(三) 几个简单的函数