Skip to content

feat: update solutions to lc problem: No.0898 #3259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions solution/0800-0899/0898.Bitwise ORs of Subarrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ tags:
```python
class Solution:
def subarrayBitwiseORs(self, arr: List[int]) -> int:
s = {0}
ans = set()
s = set()
for x in arr:
s = {x | y for y in s} | {x}
ans |= s
Expand All @@ -102,17 +102,16 @@ class Solution:
```java
class Solution {
public int subarrayBitwiseORs(int[] arr) {
Set<Integer> s = new HashSet<>();
s.add(0);
Set<Integer> ans = new HashSet<>();
Set<Integer> s = new HashSet<>();
for (int x : arr) {
Set<Integer> t = new HashSet<>();
for (int y : s) {
t.add(x | y);
}
t.add(x);
ans.addAll(t);
s = t;
ans.addAll(s);
}
return ans.size();
}
Expand All @@ -125,15 +124,16 @@ class Solution {
class Solution {
public:
int subarrayBitwiseORs(vector<int>& arr) {
unordered_set<int> s{{0}};
unordered_set<int> ans;
for (int& x : arr) {
unordered_set<int> t{{x}};
unordered_set<int> s;
for (int x : arr) {
unordered_set<int> t;
for (int y : s) {
t.insert(x | y);
}
t.insert(x);
ans.insert(t.begin(), t.end());
s = move(t);
ans.insert(s.begin(), s.end());
}
return ans.size();
}
Expand All @@ -145,16 +145,16 @@ public:
```go
func subarrayBitwiseORs(arr []int) int {
ans := map[int]bool{}
s := map[int]bool{0: true}
s := map[int]bool{}
for _, x := range arr {
t := map[int]bool{x: true}
for y := range s {
t[x|y] = true
}
s = t
for y := range s {
for y := range t {
ans[y] = true
}
s = t
}
return len(ans)
}
Expand All @@ -164,18 +164,17 @@ func subarrayBitwiseORs(arr []int) int {

```ts
function subarrayBitwiseORs(arr: number[]): number {
const s: Set<number> = new Set();
const ans: Set<number> = new Set();
const s: Set<number> = new Set();
for (const x of arr) {
const t: Set<number> = new Set();
const t: Set<number> = new Set([x]);
for (const y of s) {
t.add(x | y);
}
t.add(x);
s.clear();
for (const y of t) {
s.add(y);
ans.add(y);
s.add(y);
}
}
return ans.size;
Expand Down
29 changes: 14 additions & 15 deletions solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ The time complexity is $O(n \times \log M)$, and the space complexity is $O(n \t
```python
class Solution:
def subarrayBitwiseORs(self, arr: List[int]) -> int:
s = {0}
ans = set()
s = set()
for x in arr:
s = {x | y for y in s} | {x}
ans |= s
Expand All @@ -97,17 +97,16 @@ class Solution:
```java
class Solution {
public int subarrayBitwiseORs(int[] arr) {
Set<Integer> s = new HashSet<>();
s.add(0);
Set<Integer> ans = new HashSet<>();
Set<Integer> s = new HashSet<>();
for (int x : arr) {
Set<Integer> t = new HashSet<>();
for (int y : s) {
t.add(x | y);
}
t.add(x);
ans.addAll(t);
s = t;
ans.addAll(s);
}
return ans.size();
}
Expand All @@ -120,15 +119,16 @@ class Solution {
class Solution {
public:
int subarrayBitwiseORs(vector<int>& arr) {
unordered_set<int> s{{0}};
unordered_set<int> ans;
for (int& x : arr) {
unordered_set<int> t{{x}};
unordered_set<int> s;
for (int x : arr) {
unordered_set<int> t;
for (int y : s) {
t.insert(x | y);
}
t.insert(x);
ans.insert(t.begin(), t.end());
s = move(t);
ans.insert(s.begin(), s.end());
}
return ans.size();
}
Expand All @@ -140,16 +140,16 @@ public:
```go
func subarrayBitwiseORs(arr []int) int {
ans := map[int]bool{}
s := map[int]bool{0: true}
s := map[int]bool{}
for _, x := range arr {
t := map[int]bool{x: true}
for y := range s {
t[x|y] = true
}
s = t
for y := range s {
for y := range t {
ans[y] = true
}
s = t
}
return len(ans)
}
Expand All @@ -159,18 +159,17 @@ func subarrayBitwiseORs(arr []int) int {

```ts
function subarrayBitwiseORs(arr: number[]): number {
const s: Set<number> = new Set();
const ans: Set<number> = new Set();
const s: Set<number> = new Set();
for (const x of arr) {
const t: Set<number> = new Set();
const t: Set<number> = new Set([x]);
for (const y of s) {
t.add(x | y);
}
t.add(x);
s.clear();
for (const y of t) {
s.add(y);
ans.add(y);
s.add(y);
}
}
return ans.size;
Expand Down
9 changes: 5 additions & 4 deletions solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
class Solution {
public:
int subarrayBitwiseORs(vector<int>& arr) {
unordered_set<int> s{{0}};
unordered_set<int> ans;
for (int& x : arr) {
unordered_set<int> t{{x}};
unordered_set<int> s;
for (int x : arr) {
unordered_set<int> t;
for (int y : s) {
t.insert(x | y);
}
t.insert(x);
ans.insert(t.begin(), t.end());
s = move(t);
ans.insert(s.begin(), s.end());
}
return ans.size();
}
Expand Down
6 changes: 3 additions & 3 deletions solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
func subarrayBitwiseORs(arr []int) int {
ans := map[int]bool{}
s := map[int]bool{0: true}
s := map[int]bool{}
for _, x := range arr {
t := map[int]bool{x: true}
for y := range s {
t[x|y] = true
}
s = t
for y := range s {
for y := range t {
ans[y] = true
}
s = t
}
return len(ans)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
class Solution {
public int subarrayBitwiseORs(int[] arr) {
Set<Integer> s = new HashSet<>();
s.add(0);
Set<Integer> ans = new HashSet<>();
Set<Integer> s = new HashSet<>();
for (int x : arr) {
Set<Integer> t = new HashSet<>();
for (int y : s) {
t.add(x | y);
}
t.add(x);
ans.addAll(t);
s = t;
ans.addAll(s);
}
return ans.size();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def subarrayBitwiseORs(self, arr: List[int]) -> int:
s = {0}
ans = set()
s = set()
for x in arr:
s = {x | y for y in s} | {x}
ans |= s
Expand Down
7 changes: 3 additions & 4 deletions solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
function subarrayBitwiseORs(arr: number[]): number {
const s: Set<number> = new Set();
const ans: Set<number> = new Set();
const s: Set<number> = new Set();
for (const x of arr) {
const t: Set<number> = new Set();
const t: Set<number> = new Set([x]);
for (const y of s) {
t.add(x | y);
}
t.add(x);
s.clear();
for (const y of t) {
s.add(y);
ans.add(y);
s.add(y);
}
}
return ans.size;
Expand Down
Loading