From 6ab3755b929c7a36b352d834cb8460e762bd438f Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Tue, 19 Oct 2021 09:30:57 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.2044 (#586) No.2044.Count Number of Maximum Bitwise-OR Subsets --- .../README.md | 23 +++++++++++++++++++ .../README_EN.md | 23 +++++++++++++++++++ .../Solution.ts | 18 +++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md index ef0698bb963e9..f184a2b996249 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md @@ -117,6 +117,29 @@ class Solution { } ``` +### **TypeScript** + +```ts +function countMaxOrSubsets(nums: number[]): number { + let n = nums.length; + let max = 0; + for (let i = 0; i < n; i++) { + max |= nums[i]; + } + let ans = 0; + function dfs (pre: number, depth: number): void { + if (depth == n) { + if (pre == max) ++ans; + return; + } + dfs(pre, depth + 1); + dfs(pre | nums[depth], depth + 1); + } + dfs(0, 0); + return ans; +}; +``` + ### **C++** ```cpp diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md index d7e8c864923a8..a4f68fd316a9c 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README_EN.md @@ -107,6 +107,29 @@ class Solution { } ``` +### **TypeScript** + +```ts +function countMaxOrSubsets(nums: number[]): number { + let n = nums.length; + let max = 0; + for (let i = 0; i < n; i++) { + max |= nums[i]; + } + let ans = 0; + function dfs (pre: number, depth: number): void { + if (depth == n) { + if (pre == max) ++ans; + return; + } + dfs(pre, depth + 1); + dfs(pre | nums[depth], depth + 1); + } + dfs(0, 0); + return ans; +}; +``` + ### **C++** ```cpp diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts new file mode 100644 index 0000000000000..60da379f30e27 --- /dev/null +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.ts @@ -0,0 +1,18 @@ +function countMaxOrSubsets(nums: number[]): number { + let n = nums.length; + let max = 0; + for (let i = 0; i < n; i++) { + max |= nums[i]; + } + let ans = 0; + function dfs (pre: number, depth: number): void { + if (depth == n) { + if (pre == max) ++ans; + return; + } + dfs(pre, depth + 1); + dfs(pre | nums[depth], depth + 1); + } + dfs(0, 0); + return ans; +}; \ No newline at end of file