概述
Flatten List 列表扁平化
Description
Given a list, each element in the list can be a list or an integer.Flatten it into a simply list with integers.
/**
* // 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 List<NestedInteger> getList();
* }
*/
public class Solution {
// @param nestedList a list of NestedInteger
// @return a list of integer
public List<Integer> flatten(List<NestedInteger> nestedList) {
// Write your code here
List<Integer> result = new ArrayList<Integer>() ;
for(NestedInteger ele : nestedList ){
if(ele.isInteger()){
result.add(ele.getInteger()) ;
}else{
result.addAll(flatten(ele.getList())) ;
}
}
return result ;
}
}
最后
以上就是无辜老鼠为你收集整理的22题 Flatten List 列表扁平化的全部内容,希望文章能够帮你解决22题 Flatten List 列表扁平化所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复