我是靠谱客的博主 无辜老鼠,这篇文章主要介绍22题 Flatten List 列表扁平化,现在分享给大家,希望可以做个参考。

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.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/** * // 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题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部