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.)

答案

class Solution {
    public int subarrayBitwiseORs(int[] A) {
        Set<Integer> result_set = new HashSet<>();
        Set<Integer> lastline_set = new HashSet<>();
        
        for(int a : A) {
            Set<Integer> currline_set = new HashSet<>();
            currline_set.add(a);
            for(int s : lastline_set) {
                currline_set.add(s | a);
            }
            for(int s : currline_set) {
                result_set.add(s);
            }
            lastline_set = currline_set;
        }
        return result_set.size();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,954评论 0 10
  • 一,怎样新建 文件菜单→新建 ctrl+N 二,电子屏幕 单位:像素 分辨率:72 颜色模式:RGB tip:网页...
    WYX雨菱欣翼阅读 264评论 0 0
  • 身在静静慵懒的午后 心也随风游想到天地的尽头 昨天猝不及防的溜走 顺手带去矜贵的所有 空空的画楼没辛看到你的锦绣 ...
    江春入旧年阅读 679评论 0 0
  • 你以为前端就上面的三大特点? 是的,在这次换工作之前我真的认为就这些。 从简单开始梳理题目: 1.前端页面的构成?...
    sallon阅读 8,621评论 4 23

友情链接更多精彩内容