A Ruby Solution


  • 1
    class NumArray
    
      def initialize(nums)
        @sum = [s = 0] + nums.map { |n| s += n }
      end
    
      def sum_range(i, j)
        @sum[j + 1] - @sum[i]
      end
    
    end

Log in to reply
 

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