add ruby solution


  • 0
    S

    the recursive method but in ruby

    def depth_sum(nested_list)
       depth = 1
       depth_sum_helper(nested_list, depth)
    end
    
    
    def depth_sum_helper(nested_list, depth)
      result = 0
      nested_list.each do |ni|
        if ni.is_integer
          result += ni.get_integer * depth
        else
          sub_list = ni.get_list
          result += depth_sum_helper sub_list, depth+1
        end
      end
      result
    end
    

Log in to reply
 

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