美文网首页
Bitwise ORs of Subarrays

Bitwise ORs of Subarrays

作者: BLUE_fdf9 | 来源:发表于2018-10-31 00:53 被阅读0次

    题目
    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();
        }
    }
    

    相关文章

      网友评论

          本文标题:Bitwise ORs of Subarrays

          本文链接:https://www.haomeiwen.com/subject/yjhotqtx.html