diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/README.md b/solution/0800-0899/0898.Bitwise ORs of Subarrays/README.md index 3c316fed59309..252448058aa7c 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/README.md +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/README.md @@ -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 @@ -102,17 +102,16 @@ class Solution: ```java class Solution { public int subarrayBitwiseORs(int[] arr) { - Set s = new HashSet<>(); - s.add(0); Set ans = new HashSet<>(); + Set s = new HashSet<>(); for (int x : arr) { Set 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(); } @@ -125,15 +124,16 @@ class Solution { class Solution { public: int subarrayBitwiseORs(vector& arr) { - unordered_set s{{0}}; unordered_set ans; - for (int& x : arr) { - unordered_set t{{x}}; + unordered_set s; + for (int x : arr) { + unordered_set 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(); } @@ -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) } @@ -164,18 +164,17 @@ func subarrayBitwiseORs(arr []int) int { ```ts function subarrayBitwiseORs(arr: number[]): number { - const s: Set = new Set(); const ans: Set = new Set(); + const s: Set = new Set(); for (const x of arr) { - const t: Set = new Set(); + const t: Set = 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; diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md b/solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md index 23bf9e4ae1c2f..91f85ee4b3f97 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md @@ -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 @@ -97,17 +97,16 @@ class Solution: ```java class Solution { public int subarrayBitwiseORs(int[] arr) { - Set s = new HashSet<>(); - s.add(0); Set ans = new HashSet<>(); + Set s = new HashSet<>(); for (int x : arr) { Set 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(); } @@ -120,15 +119,16 @@ class Solution { class Solution { public: int subarrayBitwiseORs(vector& arr) { - unordered_set s{{0}}; unordered_set ans; - for (int& x : arr) { - unordered_set t{{x}}; + unordered_set s; + for (int x : arr) { + unordered_set 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(); } @@ -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) } @@ -159,18 +159,17 @@ func subarrayBitwiseORs(arr []int) int { ```ts function subarrayBitwiseORs(arr: number[]): number { - const s: Set = new Set(); const ans: Set = new Set(); + const s: Set = new Set(); for (const x of arr) { - const t: Set = new Set(); + const t: Set = 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; diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp index c4417a1fcf5c0..3c404c26d8e65 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp @@ -1,15 +1,16 @@ class Solution { public: int subarrayBitwiseORs(vector& arr) { - unordered_set s{{0}}; unordered_set ans; - for (int& x : arr) { - unordered_set t{{x}}; + unordered_set s; + for (int x : arr) { + unordered_set 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(); } diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.go b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.go index b2c176e2064ed..b31af1ef4e321 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.go +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.go @@ -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) } \ No newline at end of file diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.java b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.java index 5ff6c49e19595..1abc37ee9077a 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.java +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.java @@ -1,16 +1,15 @@ class Solution { public int subarrayBitwiseORs(int[] arr) { - Set s = new HashSet<>(); - s.add(0); Set ans = new HashSet<>(); + Set s = new HashSet<>(); for (int x : arr) { Set 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(); } diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.py b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.py index eeb068dd19f1c..872ad4ce2811b 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.py +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.py @@ -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 diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.ts b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.ts index 1794487d4b566..ab1b90f0f049c 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.ts +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.ts @@ -1,16 +1,15 @@ function subarrayBitwiseORs(arr: number[]): number { - const s: Set = new Set(); const ans: Set = new Set(); + const s: Set = new Set(); for (const x of arr) { - const t: Set = new Set(); + const t: Set = 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;