Skip to content

Commit e4aed2b

Browse files
committed
feat: add solutions to lc problem: No.2222
No.2222.Number of Ways to Select Buildings
1 parent e0077ea commit e4aed2b

File tree

6 files changed

+267
-2
lines changed

6 files changed

+267
-2
lines changed

Diff for: solution/2200-2299/2222.Number of Ways to Select Buildings/README.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,117 @@
5858

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

61+
**方法一:统计 010 和 101 的出现次数**
62+
63+
有效方案只有两种情况:$010$ 和 $101$。枚举中间数字,累计方案数。
64+
65+
时间复杂度 $O(n)$,其中 $n$ 表示 $s$ 的长度。
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
6470

6571
<!-- 这里可写当前语言的特殊实现逻辑 -->
6672

6773
```python
68-
74+
class Solution:
75+
def numberOfWays(self, s: str) -> int:
76+
n = len(s)
77+
cnt0 = s.count("0")
78+
cnt1 = n - cnt0
79+
c0 = c1 = 0
80+
ans = 0
81+
for c in s:
82+
if c == "0":
83+
ans += c1 * (cnt1 - c1)
84+
c0 += 1
85+
else:
86+
ans += c0 * (cnt0 - c0)
87+
c1 += 1
88+
return ans
6989
```
7090

7191
### **Java**
7292

7393
<!-- 这里可写当前语言的特殊实现逻辑 -->
7494

7595
```java
96+
class Solution {
97+
public long numberOfWays(String s) {
98+
int n = s.length();
99+
int cnt0 = 0;
100+
for (char c : s.toCharArray()) {
101+
if (c == '0') {
102+
++cnt0;
103+
}
104+
}
105+
int cnt1 = n - cnt0;
106+
long ans = 0;
107+
int c0 = 0, c1 = 0;
108+
for (char c : s.toCharArray()) {
109+
if (c == '0') {
110+
ans += c1 * (cnt1 - c1);
111+
++c0;
112+
} else {
113+
ans += c0 * (cnt0 - c0);
114+
++c1;
115+
}
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
long long numberOfWays(string s) {
128+
int n = s.size();
129+
int cnt0 = 0;
130+
for (char& c : s) cnt0 += c == '0';
131+
int cnt1 = n - cnt0;
132+
int c0 = 0, c1 = 0;
133+
long long ans = 0;
134+
for (char& c : s)
135+
{
136+
if (c == '0')
137+
{
138+
ans += c1 * (cnt1 - c1);
139+
++c0;
140+
}
141+
else
142+
{
143+
ans += c0 * (cnt0 - c0);
144+
++c1;
145+
}
146+
}
147+
return ans;
148+
}
149+
};
150+
```
76151
152+
### **Go**
153+
154+
```go
155+
func numberOfWays(s string) int64 {
156+
n := len(s)
157+
cnt0 := strings.Count(s, "0")
158+
cnt1 := n - cnt0
159+
c0, c1 := 0, 0
160+
ans := 0
161+
for _, c := range s {
162+
if c == '0' {
163+
ans += c1 * (cnt1 - c1)
164+
c0++
165+
} else {
166+
ans += c0 * (cnt0 - c0)
167+
c1++
168+
}
169+
}
170+
return int64(ans)
171+
}
77172
```
78173

79174
### **TypeScript**

Diff for: solution/2200-2299/2222.Number of Ways to Select Buildings/README_EN.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,102 @@ No other selection is valid. Thus, there are 6 total ways.
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def numberOfWays(self, s: str) -> int:
64+
n = len(s)
65+
cnt0 = s.count("0")
66+
cnt1 = n - cnt0
67+
c0 = c1 = 0
68+
ans = 0
69+
for c in s:
70+
if c == "0":
71+
ans += c1 * (cnt1 - c1)
72+
c0 += 1
73+
else:
74+
ans += c0 * (cnt0 - c0)
75+
c1 += 1
76+
return ans
6377
```
6478

6579
### **Java**
6680

6781
```java
82+
class Solution {
83+
public long numberOfWays(String s) {
84+
int n = s.length();
85+
int cnt0 = 0;
86+
for (char c : s.toCharArray()) {
87+
if (c == '0') {
88+
++cnt0;
89+
}
90+
}
91+
int cnt1 = n - cnt0;
92+
long ans = 0;
93+
int c0 = 0, c1 = 0;
94+
for (char c : s.toCharArray()) {
95+
if (c == '0') {
96+
ans += c1 * (cnt1 - c1);
97+
++c0;
98+
} else {
99+
ans += c0 * (cnt0 - c0);
100+
++c1;
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
long long numberOfWays(string s) {
114+
int n = s.size();
115+
int cnt0 = 0;
116+
for (char& c : s) cnt0 += c == '0';
117+
int cnt1 = n - cnt0;
118+
int c0 = 0, c1 = 0;
119+
long long ans = 0;
120+
for (char& c : s)
121+
{
122+
if (c == '0')
123+
{
124+
ans += c1 * (cnt1 - c1);
125+
++c0;
126+
}
127+
else
128+
{
129+
ans += c0 * (cnt0 - c0);
130+
++c1;
131+
}
132+
}
133+
return ans;
134+
}
135+
};
136+
```
68137
138+
### **Go**
139+
140+
```go
141+
func numberOfWays(s string) int64 {
142+
n := len(s)
143+
cnt0 := strings.Count(s, "0")
144+
cnt1 := n - cnt0
145+
c0, c1 := 0, 0
146+
ans := 0
147+
for _, c := range s {
148+
if c == '0' {
149+
ans += c1 * (cnt1 - c1)
150+
c0++
151+
} else {
152+
ans += c0 * (cnt0 - c0)
153+
c1++
154+
}
155+
}
156+
return int64(ans)
157+
}
69158
```
70159

71160
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
long long numberOfWays(string s) {
4+
int n = s.size();
5+
int cnt0 = 0;
6+
for (char& c : s) cnt0 += c == '0';
7+
int cnt1 = n - cnt0;
8+
int c0 = 0, c1 = 0;
9+
long long ans = 0;
10+
for (char& c : s)
11+
{
12+
if (c == '0')
13+
{
14+
ans += c1 * (cnt1 - c1);
15+
++c0;
16+
}
17+
else
18+
{
19+
ans += c0 * (cnt0 - c0);
20+
++c1;
21+
}
22+
}
23+
return ans;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func numberOfWays(s string) int64 {
2+
n := len(s)
3+
cnt0 := strings.Count(s, "0")
4+
cnt1 := n - cnt0
5+
c0, c1 := 0, 0
6+
ans := 0
7+
for _, c := range s {
8+
if c == '0' {
9+
ans += c1 * (cnt1 - c1)
10+
c0++
11+
} else {
12+
ans += c0 * (cnt0 - c0)
13+
c1++
14+
}
15+
}
16+
return int64(ans)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public long numberOfWays(String s) {
3+
int n = s.length();
4+
int cnt0 = 0;
5+
for (char c : s.toCharArray()) {
6+
if (c == '0') {
7+
++cnt0;
8+
}
9+
}
10+
int cnt1 = n - cnt0;
11+
long ans = 0;
12+
int c0 = 0, c1 = 0;
13+
for (char c : s.toCharArray()) {
14+
if (c == '0') {
15+
ans += c1 * (cnt1 - c1);
16+
++c0;
17+
} else {
18+
ans += c0 * (cnt0 - c0);
19+
++c1;
20+
}
21+
}
22+
return ans;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def numberOfWays(self, s: str) -> int:
3+
n = len(s)
4+
cnt0 = s.count("0")
5+
cnt1 = n - cnt0
6+
c0 = c1 = 0
7+
ans = 0
8+
for c in s:
9+
if c == "0":
10+
ans += c1 * (cnt1 - c1)
11+
c0 += 1
12+
else:
13+
ans += c0 * (cnt0 - c0)
14+
c1 += 1
15+
return ans

0 commit comments

Comments
 (0)