美文网首页
339. Nested List Weight Sum

339. Nested List Weight Sum

作者: Super_Alan | 来源:发表于2018-04-19 01:04 被阅读0次

    https://leetcode.com/problems/nested-list-weight-sum/description/

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth.

    Each element is either an integer, or a list -- whose elements may also be integers or other lists.

    Example 1:
    Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)

    Example 2:
    Given the list [1,[4,[6]]], return 27. (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 42 + 63 = 27)

    /**
     * // This is the interface that allows for creating nested lists.
     * // You should not implement it, or speculate about its implementation
     * public interface NestedInteger {
     *     // Constructor initializes an empty nested list.
     *     public NestedInteger();
     *
     *     // Constructor initializes a single integer.
     *     public NestedInteger(int value);
     *
     *     // @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();
     *
     *     // Set this NestedInteger to hold a single integer.
     *     public void setInteger(int value);
     *
     *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
     *     public void add(NestedInteger ni);
     *
     *     // @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();
     * }
     */
    

    my DFS solution

        public int depthSum(List<NestedInteger> nestedList) {
            return depthSumHelper(nestedList, 1);
        }
        
        private int depthSumHelper(List<NestedInteger> nestedList, int level) {
            int sum = 0;
            for (NestedInteger ni: nestedList) {
                if (ni.isInteger()) {
                    sum += ni.getInteger() * level;
                } else {
                    sum += depthSumHelper(ni.getList(), level + 1);
                }
            }
            return sum;
        }
    

    BFS solution

    from: https://www.jiuzhang.com/solution/nested-list-weight-sum/

    public int depthSum(List<NestedInteger> nestedList) {
            // Write your code here
            if (nestedList == null || nestedList.size() == 0) {
                return 0;
            }
            int sum = 0;
            Queue<NestedInteger> queue = new LinkedList<NestedInteger>();
            for (NestedInteger nestedInt : nestedList) {
                queue.offer(nestedInt);
            }
    
            int depth = 0;
            while (!queue.isEmpty()) {
                int size = queue.size();
                depth++;
                for (int i = 0; i < size; i++) {
                    NestedInteger nestedInt = queue.poll();
                    if (nestedInt.isInteger()) {
                        sum += nestedInt.getInteger() * depth;
                    } else {
                        for (NestedInteger innerInt : nestedInt.getList()) {
                            queue.offer(innerInt);
                        }
                    }
                }
            }
            return sum;
        }
    

    DFS recursion

    from: https://zhengyang2015.gitbooks.io/lintcode/nested_list_weight_sum_551.html

    class Node{
        int depth;
        NestedInteger nestedInteger;
        public Node(int depth, NestedInteger nestedInteger){
            this.depth = depth;
            this.nestedInteger = nestedInteger;
        }
    }
    
    public int depthSum(List<NestedInteger> nestedList) {
            Stack<Node> stack = new Stack<Node>();
            for(int i = 0; i < nestedList.size(); i++){
                stack.push(new Node(1, nestedList.get(i)));
            }
    
            int sum = 0;
            while(!stack.isEmpty()){
                Node curt = stack.pop();
                if(curt.nestedInteger.isInteger()){
                    sum += curt.depth * curt.nestedInteger.getInteger();
                }else{
                    List<NestedInteger> list = curt.nestedInteger.getList();
                    for(int i = 0; i < list.size(); i++){
                        stack.push(new Node(curt.depth + 1, list.get(i)));
                    }
                }
            }
    
            return sum;
        }
    

    相关文章

      网友评论

          本文标题:339. Nested List Weight Sum

          本文链接:https://www.haomeiwen.com/subject/plrikftx.html