[LeetCode]Bitwise ORs of Subarrays@Golang

Bitwise ORs of Subarrays
We have an array A of non-negative integers.

For every (contiguous) subarray B = [A[i], A[i+1], …, A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | … | A[j].

Return the number of possible results. (Results that occur more than once are only counted once in the final answer.)

Example

Input: [1,1,2]
Output: 3
Explanation:
The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.

Solution

func subarrayBitwiseORs(A []int) int {
    var cur,cur2 []int
    ret:= make(map[int]bool, len(A))
    for _,i := range A{
        isInCur2 := make(map[int]bool, len(cur))
        cur2 = append(cur2, i)
        ret[i], isInCur2[i] = true, true
        for _,j :=range cur{
            j |= i
            if !isInCur2[j]{
                cur2 = append(cur2, j)
                ret[j], isInCur2[j] = true, true
            }
        }
        cur, cur2 = cur2, cur[:0]
    }
    return len(ret)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值