Skip to content

Commit 4938b95

Browse files
Add files via upload
1 parent dd97d8b commit 4938b95

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)