要求:

 

给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。

 注意事项

如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。

样例:给定 [1,2,[1,2]],返回 [1,2,1,2]

给定 [4,[3,[2,[1]]]],返回 [4,3,2,1]

思路:比较简单,直接递归调用即可。

 

 

/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger { * *     // @return true if this NestedInteger holds a single integer, *     // rather than a nested list. *     public boolean isInteger(); * *     // @return the single integer that this NestedInteger holds, *     // if it holds a single integer *     // Return null if this NestedInteger holds a nested list *     public Integer getInteger(); * *     // @return the nested list that this NestedInteger holds, *     // if it holds a nested list *     // Return null if this NestedInteger holds a single integer *     public ListgetList(); * } */public class Solution {    // @param nestedList a list of NestedInteger    // @return a list of integer    public Listflatten(ListnestedList) {        // Write your code here        Listlist=new ArrayList<>();        doFlatten(nestedList,list);        return list;    }     public void doFlatten(ListnestedList,Listlist){         if(nestedList != null){             for(int i=0;i<nestedList.size();i++){                 if(nestedList.get(i).isInteger()){                     list.add(nestedList.get(i).getInteger());                 }else{                     doFlatten(nestedList.get(i).getList(),list);                 }             }         }     }}

如果有所帮助,脸皮厚求个赞~

此文章仅代表自己(本菜鸟)学习积累记录,或者学习笔记,如有侵权,请联系作者删除。人无完人,文章也一样,文笔稚嫩,在下不才,勿喷,如果有错误之处,还望指出,感激不尽~

技术之路不在一时,山高水长,纵使缓慢,驰而不息。

公众号:秦怀杂货店

 

 

 

 

 

©著作权归作者所有:来自51CTO博客作者秦怀杂货店的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. eNSP:访问控制列表 ACL
  2. zabbix监控默认的item key列表
  3. Python列表操作最全面总结
  4. 元组类型内置方法
  5. 字典类型
  6. 列表类型内置方法
  7. Python中列表、元组、字典有何区别?Python学习!
  8. python 基础语法1
  9. 2021-04-10:给定两个可能有环也可能无环的单链表,头节点head1和hea

随机推荐

  1. Python Django对接企业微信第三方服务回
  2. python 中的排序大法
  3. Python Flask WTForms:如何在视图中动态禁
  4. 从另一个Python脚本调用Python脚本的最佳
  5. python3如何打印进度条
  6. 基于fastai的分类网络
  7. 关于Python的编码注释# -*- coding:utf-8
  8. Python列表以及列表的处理方法
  9. AttributeError:“MatrixFactorizationMo
  10. 小白学Python---面向对象02