Skip to content

Commit f180226

Browse files
committed
feat: add solutions to lc problems
* No.1021.Remove Outermost Parentheses * No.2044.Count Number of Maximum Bitwise-OR Subsets
1 parent 374f4b6 commit f180226

File tree

6 files changed

+306
-2
lines changed

6 files changed

+306
-2
lines changed

solution/1000-1099/1021.Remove Outermost Parentheses/README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,20 @@
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```python
73-
73+
class Solution:
74+
def removeOuterParentheses(self, s: str) -> str:
75+
ans = []
76+
cnt = 0
77+
for c in s:
78+
if c == '(':
79+
cnt += 1
80+
if cnt > 1:
81+
ans.append(c)
82+
else:
83+
cnt -= 1
84+
if cnt > 0:
85+
ans.append(c)
86+
return ''.join(ans)
7487
```
7588

7689
### **Java**
@@ -122,6 +135,29 @@ public:
122135
};
123136
```
124137
138+
### **Go**
139+
140+
```go
141+
func removeOuterParentheses(s string) string {
142+
ans := []rune{}
143+
cnt := 0
144+
for _, c := range s {
145+
if c == '(' {
146+
cnt++
147+
if cnt > 1 {
148+
ans = append(ans, c)
149+
}
150+
} else {
151+
cnt--
152+
if cnt > 0 {
153+
ans = append(ans, c)
154+
}
155+
}
156+
}
157+
return string(ans)
158+
}
159+
```
160+
125161
### **TypeScript**
126162

127163
```ts

solution/1000-1099/1021.Remove Outermost Parentheses/README_EN.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,20 @@ After removing outer parentheses of each part, this is &quot;&quot; + &quot;&quo
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def removeOuterParentheses(self, s: str) -> str:
68+
ans = []
69+
cnt = 0
70+
for c in s:
71+
if c == '(':
72+
cnt += 1
73+
if cnt > 1:
74+
ans.append(c)
75+
else:
76+
cnt -= 1
77+
if cnt > 0:
78+
ans.append(c)
79+
return ''.join(ans)
6780
```
6881

6982
### **Java**
@@ -113,6 +126,29 @@ public:
113126
};
114127
```
115128
129+
### **Go**
130+
131+
```go
132+
func removeOuterParentheses(s string) string {
133+
ans := []rune{}
134+
cnt := 0
135+
for _, c := range s {
136+
if c == '(' {
137+
cnt++
138+
if cnt > 1 {
139+
ans = append(ans, c)
140+
}
141+
} else {
142+
cnt--
143+
if cnt > 0 {
144+
ans = append(ans, c)
145+
}
146+
}
147+
}
148+
return string(ans)
149+
}
150+
```
151+
116152
### **TypeScript**
117153

118154
```ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func removeOuterParentheses(s string) string {
2+
ans := []rune{}
3+
cnt := 0
4+
for _, c := range s {
5+
if c == '(' {
6+
cnt++
7+
if cnt > 1 {
8+
ans = append(ans, c)
9+
}
10+
} else {
11+
cnt--
12+
if cnt > 0 {
13+
ans = append(ans, c)
14+
}
15+
}
16+
}
17+
return string(ans)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def removeOuterParentheses(self, s: str) -> str:
3+
ans = []
4+
cnt = 0
5+
for c in s:
6+
if c == '(':
7+
cnt += 1
8+
if cnt > 1:
9+
ans.append(c)
10+
else:
11+
cnt -= 1
12+
if cnt > 0:
13+
ans.append(c)
14+
return ''.join(ans)

solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/README.md

+104
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,16 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:DFS**
62+
6163
简单 DFS。可以预先算出按位或的最大值 mx,然后 DFS 搜索按位或结果等于 mx 的所有子集数。也可以在 DFS 搜索中逐渐更新 mx 与对应的子集数。
6264

65+
时间复杂度 $O(2^n)$。
66+
67+
**方法二:二进制枚举**
68+
69+
时间复杂度 $O(n*2^n)$。
70+
6371
<!-- tabs:start -->
6472

6573
### **Python3**
@@ -105,6 +113,25 @@ class Solution:
105113
return ans
106114
```
107115

116+
```python
117+
class Solution:
118+
def countMaxOrSubsets(self, nums: List[int]) -> int:
119+
n = len(nums)
120+
ans = 0
121+
mx = 0
122+
for mask in range(1 << n):
123+
t = 0
124+
for i, v in enumerate(nums):
125+
if (mask >> i) & 1:
126+
t |= v
127+
if mx < t:
128+
mx = t
129+
ans = 1
130+
elif mx == t:
131+
ans += 1
132+
return ans
133+
```
134+
108135
### **Java**
109136

110137
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -166,6 +193,31 @@ class Solution {
166193
}
167194
```
168195

196+
```java
197+
class Solution {
198+
public int countMaxOrSubsets(int[] nums) {
199+
int n = nums.length;
200+
int ans = 0;
201+
int mx = 0;
202+
for (int mask = 1; mask < 1 << n; ++mask) {
203+
int t = 0;
204+
for (int i = 0; i < n; ++i) {
205+
if (((mask >> i) & 1) == 1) {
206+
t |= nums[i];
207+
}
208+
}
209+
if (mx < t) {
210+
mx = t;
211+
ans = 1;
212+
} else if (mx == t) {
213+
++ans;
214+
}
215+
}
216+
return ans;
217+
}
218+
}
219+
```
220+
169221
### **TypeScript**
170222

171223
```ts
@@ -271,6 +323,35 @@ public:
271323
};
272324
```
273325

326+
```cpp
327+
class Solution {
328+
public:
329+
int countMaxOrSubsets(vector<int>& nums) {
330+
int n = nums.size();
331+
int ans = 0;
332+
int mx = 0;
333+
for (int mask = 1; mask < 1 << n; ++mask)
334+
{
335+
int t = 0;
336+
for (int i = 0; i < n; ++i)
337+
{
338+
if ((mask >> i) & 1)
339+
{
340+
t |= nums[i];
341+
}
342+
}
343+
if (mx < t)
344+
{
345+
mx = t;
346+
ans = 1;
347+
}
348+
else if (mx == t) ++ans;
349+
}
350+
return ans;
351+
}
352+
};
353+
```
354+
274355
### **Go**
275356
276357
```go
@@ -297,6 +378,29 @@ func countMaxOrSubsets(nums []int) int {
297378
}
298379
```
299380

381+
```go
382+
func countMaxOrSubsets(nums []int) int {
383+
n := len(nums)
384+
ans := 0
385+
mx := 0
386+
for mask := 1; mask < 1<<n; mask++ {
387+
t := 0
388+
for i, v := range nums {
389+
if ((mask >> i) & 1) == 1 {
390+
t |= v
391+
}
392+
}
393+
if mx < t {
394+
mx = t
395+
ans = 1
396+
} else if mx == t {
397+
ans++
398+
}
399+
}
400+
return ans
401+
}
402+
```
403+
300404
```go
301405
func countMaxOrSubsets(nums []int) int {
302406
mx, ans := 0, 0

0 commit comments

Comments
 (0)