LintCode python 小白-简单题-22平面列表
来源:互联网 发布:淘宝的美版iphone7渠道 编辑:程序博客网 时间:2024/06/11 06:11
题目:给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。
注意事项:如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。
样例:
给定 [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)
阅读全文
1 0
- LintCode python 小白-简单题-22平面列表
- LintCode 22 平面列表
- (lintcode)第22题 平面列表
- LintCode之22 平面列表
- LintCode python 小白-简单题-627最长回文串
- LintCode python 小白-简单题-638-Strings Homomorphism
- LintCode python 小白-简单题-569各位相加
- LintCode python 小白-简单题-413-反转整数
- LintCode python 小白-简单题-2 尾部的0
- LintCode python 小白-简单题-445余弦相识度
- LintCode:平面列表
- LintCode 【平面列表】
- Lintcode 平面列表
- 平面列表-LintCode
- LintCode 平面列表
- lintcode 22.平面列表
- Lintcode:平面列表
- LintCode python 小白-简单题-646 First Position Unique Character
- Android_Manager:PackageManager
- Path Sum
- HDU 6035 Colorful Tree(树形DP)
- Set 不重复实现原理
- 详述获取字节码文件及其内容的方法
- LintCode python 小白-简单题-22平面列表
- poj 3683 2SAT入门+挑战模板
- 《Drools7.0.0.Final规则引擎教程》第4章 4.4 LHS简介&Pattern
- 冒泡排序、插入排序和选择排序
- 百度、腾讯和阿里内部的级别和薪资待遇是什么样的?
- 高并发的核心技术-幂等的实现方案
- 原生js实现检测对象变化
- 基于Nsight Eclipse Edition的高斯滤波的实现(一)
- 模块--时间--日历