Short and Clean C++ solution


  • 1
    W
    class Solution {
    public:
        int depthSum(const vector<NestedInteger>& nestedList, int depth = 1) {
            int sum = 0;
            for (const NestedInteger& ni : nestedList) {
               if (ni.isInteger()) {
                   sum += ni.getInteger() * depth;
               } else {
                   sum += depthSum(ni.getList(), depth+1);
               }
            }
            return sum;
        }
    };
    

  • 0

    Use default argument make it really clean, not sure if this is allowed. However, you solution is great.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.