Nested List Weight Sum 551

Question

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

Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1, 4 1 2 + 1 2 1 = 10) 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)

Solution

Recursion: 遍历list,当前元素如果是数,则将其乘以当前深度并加到sum里,若当前元素是list,则递归遍历该list,并将深度加1。

DFS: 用一个stack来实现。遍历list,将当前元素和深度加入stack中。依次取出栈顶元素,若取出元素为数,则将其乘以其深度并加入sum,若取出元素是list,则将该list中元素依次加入stack中,同时这些元素的深度比之前取出元素深度+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 List<NestedInteger> getList();
 * }
 */
class Node{
    int depth;
    NestedInteger nestedInteger;
    public Node(int depth, NestedInteger nestedInteger){
        this.depth = depth;
        this.nestedInteger = nestedInteger;
    }
}

public class Solution {
    //Recursion version
    // public int depthSum(List<NestedInteger> nestedList) {
    //     // Write your code here
    //     return helper(nestedList, 1);
    // }

    // private int helper(List<NestedInteger> nestedList, int depth){
    //     int n = nestedList.size();
    //     int sum = 0;
    //     for(int i = 0; i < n; i++){
    //         if(nestedList.get(i).isInteger()){
    //             sum += depth * nestedList.get(i).getInteger();
    //         }else{
    //             sum += helper(nestedList.get(i).getList(), depth + 1);
    //         }
    //     }
    //     return sum;
    // }

    //DFS version
    public int depthSum(List<NestedInteger> nestedList) {
        // Write your code here
        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;
    }
}

results matching ""

    No results matching ""