【问题标题】:Given an array of 0s and 1s only and another array of ranges. return an array with number of zeroes in each set of range给定一个仅包含 0 和 1 的数组以及另一个范围数组。返回一个数组,其中每组范围内的零个数
【发布时间】:2021-06-29 22:42:28
【问题描述】:

仅给定一个由 0 和 1 组成的数组以及另一个范围数组。 返回一个数组,其中每组范围内的零个数。

输入

bits = [1,1,0,1,0,1,1,0,1,0,1]
ranges = [[0,3],[1,4],[5,7],[4,8],[7,10]]

输出:

result = [1,2,1,2,2]

【问题讨论】:

    标签: arrays algorithm


    【解决方案1】:

    在位数组中创建一个零的数量的辅助数组,直到该点。

    bits = [1,1,0,1,0,1,1,0,1,0,1]
    sum  = [0,0,1,1,2,2,2,3,3,4,4]
    

    [0,3] 的结果就是 sum[3]-sum[0]。

    Make a helper array with the running sum of zeroes.
    then subtract the the value at start of range from the value at end of range.
    push result
    repeat
    

    【讨论】:

      猜你喜欢
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多