Flatten List

Given a list, each element in the list can be a list or integer. flatten it into a simply list with integers.

Example

Example 1:
	Input:  [[1,1],2,[1,1]]
	Output: [1,1,2,1,1]
	
	Explanation:
	flatten it into a simply list with integers.

Example 2:
	Input:  [1,2,[1,2]]
	Output:[1,2,1,2]
	
	Explanation:  
	flatten it into a simply list with integers.

Example 3:
	Input: [4,[3,[2,[1]]]]
	Output:[4,3,2,1]
	
	Explanation: 
	flatten it into a simply list with integers.

Challenge

Do it in non-recursive.

Notice

If the element in the given list is a list, it can contain list too.

思路:dfs,处理子问题,分两种情况,一种是integer直接加入,另外一种是list,dfs处理,两种都处理完了,处理下一个index的东西;

/**
 * // 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) {
        List<Integer> list = new ArrayList<Integer>();
        if(nestedList == null) {
            return list;
        }
        dfs(nestedList, list, 0);
        return list;
    }
    
    private void dfs(List<NestedInteger> nestedList, List<Integer> list, int index) {
        if(index == nestedList.size()) {
            return;
        }
        
        NestedInteger integer = nestedList.get(index);
        if(integer.isInteger()){
            list.add(integer.getInteger());
        } else { 
            // integer is list;
            List<NestedInteger> newNestedList = integer.getList();
            dfs(newNestedList, list, 0);
        }
        dfs(nestedList, list, index+1);
    }
}

思路2: 用stack来模拟dfs;注意stack里面存的是list的iterator指针;stack保存的是当前第N层的指针,然后cur挪到第N+1层的指针,最后处理完里面的层之后,cur = stack.pop()回到第N层的指针,继续往下走;

/**
 * // 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) {
        List<Integer> list = new ArrayList<>();
        if(nestedList == null || nestedList.size() == 0) return list;
        
        Stack<Iterator<NestedInteger>> stack = new Stack();
        Iterator<NestedInteger> cur = nestedList.iterator();
        stack.push(cur);
        while(!stack.isEmpty() || cur.hasNext()) {
            if(cur.hasNext()){
               NestedInteger it = cur.next();
                if(it.isInteger()){
                    list.add(it.getInteger());
                } else {
                    stack.push(cur); // push第N层现在的指针;
                    cur = it.getList().iterator(); // 进入里面N+1一层的iterator,处理N+1层;
                } 
            } else {
                cur = stack.pop(); // 还原到第N层的指针,然后处理下一个;
            }
        }
        return list;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值