@@ -84,8 +84,8 @@ The time complexity is $O(n \times \log M)$, and the space complexity is $O(n \t
84
84
``` python
85
85
class Solution :
86
86
def subarrayBitwiseORs (self , arr : List[int ]) -> int :
87
- s = {0 }
88
87
ans = set ()
88
+ s = set ()
89
89
for x in arr:
90
90
s = {x | y for y in s} | {x}
91
91
ans |= s
@@ -97,17 +97,16 @@ class Solution:
97
97
``` java
98
98
class Solution {
99
99
public int subarrayBitwiseORs (int [] arr ) {
100
- Set<Integer > s = new HashSet<> ();
101
- s. add(0 );
102
100
Set<Integer > ans = new HashSet<> ();
101
+ Set<Integer > s = new HashSet<> ();
103
102
for (int x : arr) {
104
103
Set<Integer > t = new HashSet<> ();
105
104
for (int y : s) {
106
105
t. add(x | y);
107
106
}
108
107
t. add(x);
108
+ ans. addAll(t);
109
109
s = t;
110
- ans. addAll(s);
111
110
}
112
111
return ans. size();
113
112
}
@@ -120,15 +119,16 @@ class Solution {
120
119
class Solution {
121
120
public:
122
121
int subarrayBitwiseORs(vector<int >& arr) {
123
- unordered_set<int > s{{0}};
124
122
unordered_set<int > ans;
125
- for (int& x : arr) {
126
- unordered_set<int > t{{x}};
123
+ unordered_set<int > s;
124
+ for (int x : arr) {
125
+ unordered_set<int > t;
127
126
for (int y : s) {
128
127
t.insert(x | y);
129
128
}
129
+ t.insert(x);
130
+ ans.insert(t.begin(), t.end());
130
131
s = move(t);
131
- ans.insert(s.begin(), s.end());
132
132
}
133
133
return ans.size();
134
134
}
@@ -140,16 +140,16 @@ public:
140
140
```go
141
141
func subarrayBitwiseORs(arr []int) int {
142
142
ans := map[int]bool{}
143
- s := map[int]bool{0: true }
143
+ s := map[int]bool{}
144
144
for _, x := range arr {
145
145
t := map[int]bool{x: true}
146
146
for y := range s {
147
147
t[x|y] = true
148
148
}
149
- s = t
150
- for y := range s {
149
+ for y := range t {
151
150
ans[y] = true
152
151
}
152
+ s = t
153
153
}
154
154
return len(ans)
155
155
}
@@ -159,18 +159,17 @@ func subarrayBitwiseORs(arr []int) int {
159
159
160
160
``` ts
161
161
function subarrayBitwiseORs(arr : number []): number {
162
- const s: Set <number > = new Set ();
163
162
const ans: Set <number > = new Set ();
163
+ const s: Set <number > = new Set ();
164
164
for (const x of arr ) {
165
- const t: Set <number > = new Set ();
165
+ const t: Set <number > = new Set ([ x ] );
166
166
for (const y of s ) {
167
167
t .add (x | y );
168
168
}
169
- t .add (x );
170
169
s .clear ();
171
170
for (const y of t ) {
172
- s .add (y );
173
171
ans .add (y );
172
+ s .add (y );
174
173
}
175
174
}
176
175
return ans .size ;
0 commit comments