File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 这道题目最直接的方法明显是n3的时间复杂度
2
+ // 稍微优化一点就是n2的时间复杂度,常数的空间复杂度
3
+ // 加速的思路可以做到线性的时间复杂度,附参考链接如下
4
+ // https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165859/C%2B%2B-O(kN)-solution
5
+
6
+ // Runtime: 1380 ms, faster than 41.00% of C++ online submissions for Bitwise ORs of Subarrays.
7
+ // Memory Usage: 299.4 MB, less than 66.67% of C++ online submissions for Bitwise ORs of Subarrays.
8
+
9
+ class Solution
10
+ {
11
+ public:
12
+ int subarrayBitwiseORs (vector<int >& A)
13
+ {
14
+ unordered_set<int > res, hs1;
15
+ for (int item : A)
16
+ {
17
+ unordered_set<int > hs2;
18
+ for (int iitem : hs1)
19
+ hs2.insert (item | iitem);
20
+ hs2.insert (item);
21
+
22
+ hs1 = hs2;
23
+
24
+ for (int iitem : hs2)
25
+ res.insert (iitem);
26
+ }
27
+ return res.size ();
28
+ }
29
+ };
You can’t perform that action at this time.
0 commit comments