Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d273d27

Browse files
feat: update solutions to lc problem: No.0898 (doocs#3259)
No.0898.Bitwise ORs of Subarrays
1 parent dac1768 commit d273d27

File tree

7 files changed

+42
-45
lines changed

7 files changed

+42
-45
lines changed

‎solution/0800-0899/0898.Bitwise ORs of Subarrays/README.md‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ tags:
8989
```python
9090
class Solution:
9191
def subarrayBitwiseORs(self, arr: List[int]) -> int:
92-
s = {0}
9392
ans = set()
93+
s = set()
9494
for x in arr:
9595
s = {x | y for y in s} | {x}
9696
ans |= s
@@ -102,17 +102,16 @@ class Solution:
102102
```java
103103
class Solution {
104104
public int subarrayBitwiseORs(int[] arr) {
105-
Set<Integer> s = new HashSet<>();
106-
s.add(0);
107105
Set<Integer> ans = new HashSet<>();
106+
Set<Integer> s = new HashSet<>();
108107
for (int x : arr) {
109108
Set<Integer> t = new HashSet<>();
110109
for (int y : s) {
111110
t.add(x | y);
112111
}
113112
t.add(x);
113+
ans.addAll(t);
114114
s = t;
115-
ans.addAll(s);
116115
}
117116
return ans.size();
118117
}
@@ -125,15 +124,16 @@ class Solution {
125124
class Solution {
126125
public:
127126
int subarrayBitwiseORs(vector<int>& arr) {
128-
unordered_set<int> s{{0}};
129127
unordered_set<int> ans;
130-
for (int& x : arr) {
131-
unordered_set<int> t{{x}};
128+
unordered_set<int> s;
129+
for (int x : arr) {
130+
unordered_set<int> t;
132131
for (int y : s) {
133132
t.insert(x | y);
134133
}
134+
t.insert(x);
135+
ans.insert(t.begin(), t.end());
135136
s = move(t);
136-
ans.insert(s.begin(), s.end());
137137
}
138138
return ans.size();
139139
}
@@ -145,16 +145,16 @@ public:
145145
```go
146146
func subarrayBitwiseORs(arr []int) int {
147147
ans := map[int]bool{}
148-
s := map[int]bool{0: true}
148+
s := map[int]bool{}
149149
for _, x := range arr {
150150
t := map[int]bool{x: true}
151151
for y := range s {
152152
t[x|y] = true
153153
}
154-
s = t
155-
for y := range s {
154+
for y := range t {
156155
ans[y] = true
157156
}
157+
s = t
158158
}
159159
return len(ans)
160160
}
@@ -164,18 +164,17 @@ func subarrayBitwiseORs(arr []int) int {
164164

165165
```ts
166166
function subarrayBitwiseORs(arr: number[]): number {
167-
const s: Set<number> = new Set();
168167
const ans: Set<number> = new Set();
168+
const s: Set<number> = new Set();
169169
for (const x of arr) {
170-
const t: Set<number> = new Set();
170+
const t: Set<number> = new Set([x]);
171171
for (const y of s) {
172172
t.add(x | y);
173173
}
174-
t.add(x);
175174
s.clear();
176175
for (const y of t) {
177-
s.add(y);
178176
ans.add(y);
177+
s.add(y);
179178
}
180179
}
181180
return ans.size;

‎solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ The time complexity is $O(n \times \log M),ドル and the space complexity is $O(n \t
8484
```python
8585
class Solution:
8686
def subarrayBitwiseORs(self, arr: List[int]) -> int:
87-
s = {0}
8887
ans = set()
88+
s = set()
8989
for x in arr:
9090
s = {x | y for y in s} | {x}
9191
ans |= s
@@ -97,17 +97,16 @@ class Solution:
9797
```java
9898
class Solution {
9999
public int subarrayBitwiseORs(int[] arr) {
100-
Set<Integer> s = new HashSet<>();
101-
s.add(0);
102100
Set<Integer> ans = new HashSet<>();
101+
Set<Integer> s = new HashSet<>();
103102
for (int x : arr) {
104103
Set<Integer> t = new HashSet<>();
105104
for (int y : s) {
106105
t.add(x | y);
107106
}
108107
t.add(x);
108+
ans.addAll(t);
109109
s = t;
110-
ans.addAll(s);
111110
}
112111
return ans.size();
113112
}
@@ -120,15 +119,16 @@ class Solution {
120119
class Solution {
121120
public:
122121
int subarrayBitwiseORs(vector<int>& arr) {
123-
unordered_set<int> s{{0}};
124122
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;
127126
for (int y : s) {
128127
t.insert(x | y);
129128
}
129+
t.insert(x);
130+
ans.insert(t.begin(), t.end());
130131
s = move(t);
131-
ans.insert(s.begin(), s.end());
132132
}
133133
return ans.size();
134134
}
@@ -140,16 +140,16 @@ public:
140140
```go
141141
func subarrayBitwiseORs(arr []int) int {
142142
ans := map[int]bool{}
143-
s := map[int]bool{0: true}
143+
s := map[int]bool{}
144144
for _, x := range arr {
145145
t := map[int]bool{x: true}
146146
for y := range s {
147147
t[x|y] = true
148148
}
149-
s = t
150-
for y := range s {
149+
for y := range t {
151150
ans[y] = true
152151
}
152+
s = t
153153
}
154154
return len(ans)
155155
}
@@ -159,18 +159,17 @@ func subarrayBitwiseORs(arr []int) int {
159159

160160
```ts
161161
function subarrayBitwiseORs(arr: number[]): number {
162-
const s: Set<number> = new Set();
163162
const ans: Set<number> = new Set();
163+
const s: Set<number> = new Set();
164164
for (const x of arr) {
165-
const t: Set<number> = new Set();
165+
const t: Set<number> = new Set([x]);
166166
for (const y of s) {
167167
t.add(x | y);
168168
}
169-
t.add(x);
170169
s.clear();
171170
for (const y of t) {
172-
s.add(y);
173171
ans.add(y);
172+
s.add(y);
174173
}
175174
}
176175
return ans.size;

‎solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
22
public:
33
int subarrayBitwiseORs(vector<int>& arr) {
4-
unordered_set<int> s{{0}};
54
unordered_set<int> ans;
6-
for (int& x : arr) {
7-
unordered_set<int> t{{x}};
5+
unordered_set<int> s;
6+
for (int x : arr) {
7+
unordered_set<int> t;
88
for (int y : s) {
99
t.insert(x | y);
1010
}
11+
t.insert(x);
12+
ans.insert(t.begin(), t.end());
1113
s = move(t);
12-
ans.insert(s.begin(), s.end());
1314
}
1415
return ans.size();
1516
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
func subarrayBitwiseORs(arr []int) int {
22
ans := map[int]bool{}
3-
s := map[int]bool{0: true}
3+
s := map[int]bool{}
44
for _, x := range arr {
55
t := map[int]bool{x: true}
66
for y := range s {
77
t[x|y] = true
88
}
9-
s = t
10-
for y := range s {
9+
for y := range t {
1110
ans[y] = true
1211
}
12+
s = t
1313
}
1414
return len(ans)
1515
}

‎solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class Solution {
22
public int subarrayBitwiseORs(int[] arr) {
3-
Set<Integer> s = new HashSet<>();
4-
s.add(0);
53
Set<Integer> ans = new HashSet<>();
4+
Set<Integer> s = new HashSet<>();
65
for (int x : arr) {
76
Set<Integer> t = new HashSet<>();
87
for (int y : s) {
98
t.add(x | y);
109
}
1110
t.add(x);
11+
ans.addAll(t);
1212
s = t;
13-
ans.addAll(s);
1413
}
1514
return ans.size();
1615
}

‎solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def subarrayBitwiseORs(self, arr: List[int]) -> int:
3-
s = {0}
43
ans = set()
4+
s = set()
55
for x in arr:
66
s = {x | y for y in s} | {x}
77
ans |= s

‎solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
function subarrayBitwiseORs(arr: number[]): number {
2-
const s: Set<number> = new Set();
32
const ans: Set<number> = new Set();
3+
const s: Set<number> = new Set();
44
for (const x of arr) {
5-
const t: Set<number> = new Set();
5+
const t: Set<number> = new Set([x]);
66
for (const y of s) {
77
t.add(x | y);
88
}
9-
t.add(x);
109
s.clear();
1110
for (const y of t) {
12-
s.add(y);
1311
ans.add(y);
12+
s.add(y);
1413
}
1514
}
1615
return ans.size;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /