Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5327c60

Browse files
committed
feat: add solutions to lc problem: No.2315
No.2315.Count Asterisks
1 parent a26365c commit 5327c60

File tree

10 files changed

+184
-134
lines changed

10 files changed

+184
-134
lines changed

‎solution/2300-2399/2315.Count Asterisks/README.md‎

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:模拟**
54+
55+
我们定义一个整型变量 $ok,ドル表示遇到 `*` 时是否能计数,初始时 $ok=1,ドル表示可以计数。
56+
57+
遍历字符串 $s,ドル如果遇到 `*`,则根据 $ok$ 的值决定是否计数,如果遇到 `|`,则 $ok$ 的值取反。
58+
59+
最后返回计数的结果。
60+
61+
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
62+
5363
<!-- tabs:start -->
5464

5565
### **Python3**
@@ -59,13 +69,12 @@
5969
```python
6070
class Solution:
6171
def countAsterisks(self, s: str) -> int:
62-
ans= t = 0
72+
ans, ok = 0, 1
6373
for c in s:
64-
if c == '|':
65-
t ^= 1
66-
elif c == '*':
67-
if t == 0:
68-
ans += 1
74+
if c == "*":
75+
ans += ok
76+
elif c == "|":
77+
ok ^= 1
6978
return ans
7079
```
7180

@@ -76,14 +85,13 @@ class Solution:
7685
```java
7786
class Solution {
7887
public int countAsterisks(String s) {
79-
int ans = 0, t = 0;
80-
for (char c : s.toCharArray()) {
81-
if (c == '|') {
82-
t ^= 1;
83-
} else if (c == '*') {
84-
if (t == 0) {
85-
++ans;
86-
}
88+
int ans = 0;
89+
for (int i = 0, ok = 1; i < s.length(); ++i) {
90+
char c = s.charAt(i);
91+
if (c == '*') {
92+
ans += ok;
93+
} else if (c == '|') {
94+
ok ^= 1;
8795
}
8896
}
8997
return ans;
@@ -97,12 +105,13 @@ class Solution {
97105
class Solution {
98106
public:
99107
int countAsterisks(string s) {
100-
int ans = 0, t = 0;
108+
int ans = 0, ok = 1;
101109
for (char& c : s) {
102-
if (c == '|')
103-
t ^= 1;
104-
else if (c == '*')
105-
ans += t == 0;
110+
if (c == '*') {
111+
ans += ok;
112+
} else if (c == '|') {
113+
ok ^= 1;
114+
}
106115
}
107116
return ans;
108117
}
@@ -112,18 +121,16 @@ public:
112121
### **Go**
113122
114123
```go
115-
func countAsterisks(s string) int {
116-
ans, t := 0, 0
124+
func countAsterisks(s string) (ans int) {
125+
ok := 1
117126
for _, c := range s {
118-
if c == '|' {
119-
t ^= 1
120-
} else if c == '*' {
121-
if t == 0 {
122-
ans++
123-
}
127+
if c == '*' {
128+
ans += ok
129+
} else if c == '|' {
130+
ok ^= 1
124131
}
125132
}
126-
return ans
133+
return
127134
}
128135
```
129136

@@ -132,12 +139,12 @@ func countAsterisks(s string) int {
132139
```ts
133140
function countAsterisks(s: string): number {
134141
let ans = 0;
135-
let flag = true;
142+
let ok = 1;
136143
for (const c of s) {
137-
if (c === '|') {
138-
flag=!flag;
139-
} else if (c === '*'&&flag) {
140-
ans++;
144+
if (c === '*') {
145+
ans+=ok;
146+
} else if (c === '|') {
147+
ok^=1;
141148
}
142149
}
143150
return ans;
@@ -150,12 +157,12 @@ function countAsterisks(s: string): number {
150157
impl Solution {
151158
pub fn count_asterisks(s: String) -> i32 {
152159
let mut ans = 0;
153-
let mut flag = true;
160+
let mut ok = 1;
154161
for &c in s.as_bytes() {
155-
if c == b'|' {
156-
flag=!flag;
157-
} else if c == b'*'&&flag {
158-
ans+= 1;
162+
if c == b'*' {
163+
ans+=ok
164+
} else if c == b'|' {
165+
ok^= 1
159166
}
160167
}
161168
ans
@@ -166,20 +173,38 @@ impl Solution {
166173
### **C**
167174

168175
```c
169-
int countAsterisks(char *s) {
176+
int countAsterisks(char * s){
170177
int ans = 0;
171-
int flag = 1;
178+
int ok = 1;
172179
for (int i = 0; s[i]; i++) {
173-
if (s[i] == '|') {
174-
flag = !flag;
175-
} else if (s[i] == '*' && flag) {
176-
ans++;
180+
if (s[i] == '*') {
181+
ans += ok;
182+
} else if (s[i] == '|') {
183+
ok ^= 1;
177184
}
178185
}
179186
return ans;
180187
}
181188
```
182189
190+
### **C#**
191+
192+
```cs
193+
public class Solution {
194+
public int CountAsterisks(string s) {
195+
int ans = 0, ok = 1;
196+
foreach (char c in s) {
197+
if (c == '*') {
198+
ans += ok;
199+
} else if (c == '|') {
200+
ok ^= 1;
201+
}
202+
}
203+
return ans;
204+
}
205+
}
206+
```
207+
183208
### **...**
184209

185210
```

‎solution/2300-2399/2315.Count Asterisks/README_EN.md‎

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ There are 2 asterisks considered. Therefore, we return 2.</pre>
5454
```python
5555
class Solution:
5656
def countAsterisks(self, s: str) -> int:
57-
ans= t = 0
57+
ans, ok = 0, 1
5858
for c in s:
59-
if c == '|':
60-
t ^= 1
61-
elif c == '*':
62-
if t == 0:
63-
ans += 1
59+
if c == "*":
60+
ans += ok
61+
elif c == "|":
62+
ok ^= 1
6463
return ans
6564
```
6665

@@ -69,14 +68,13 @@ class Solution:
6968
```java
7069
class Solution {
7170
public int countAsterisks(String s) {
72-
int ans = 0, t = 0;
73-
for (char c : s.toCharArray()) {
74-
if (c == '|') {
75-
t ^= 1;
76-
} else if (c == '*') {
77-
if (t == 0) {
78-
++ans;
79-
}
71+
int ans = 0;
72+
for (int i = 0, ok = 1; i < s.length(); ++i) {
73+
char c = s.charAt(i);
74+
if (c == '*') {
75+
ans += ok;
76+
} else if (c == '|') {
77+
ok ^= 1;
8078
}
8179
}
8280
return ans;
@@ -90,12 +88,13 @@ class Solution {
9088
class Solution {
9189
public:
9290
int countAsterisks(string s) {
93-
int ans = 0, t = 0;
91+
int ans = 0, ok = 1;
9492
for (char& c : s) {
95-
if (c == '|')
96-
t ^= 1;
97-
else if (c == '*')
98-
ans += t == 0;
93+
if (c == '*') {
94+
ans += ok;
95+
} else if (c == '|') {
96+
ok ^= 1;
97+
}
9998
}
10099
return ans;
101100
}
@@ -105,18 +104,16 @@ public:
105104
### **Go**
106105
107106
```go
108-
func countAsterisks(s string) int {
109-
ans, t := 0, 0
107+
func countAsterisks(s string) (ans int) {
108+
ok := 1
110109
for _, c := range s {
111-
if c == '|' {
112-
t ^= 1
113-
} else if c == '*' {
114-
if t == 0 {
115-
ans++
116-
}
110+
if c == '*' {
111+
ans += ok
112+
} else if c == '|' {
113+
ok ^= 1
117114
}
118115
}
119-
return ans
116+
return
120117
}
121118
```
122119

@@ -125,12 +122,12 @@ func countAsterisks(s string) int {
125122
```ts
126123
function countAsterisks(s: string): number {
127124
let ans = 0;
128-
let flag = true;
125+
let ok = 1;
129126
for (const c of s) {
130-
if (c === '|') {
131-
flag=!flag;
132-
} else if (c === '*'&&flag) {
133-
ans++;
127+
if (c === '*') {
128+
ans+=ok;
129+
} else if (c === '|') {
130+
ok^=1;
134131
}
135132
}
136133
return ans;
@@ -143,12 +140,12 @@ function countAsterisks(s: string): number {
143140
impl Solution {
144141
pub fn count_asterisks(s: String) -> i32 {
145142
let mut ans = 0;
146-
let mut flag = true;
143+
let mut ok = 1;
147144
for &c in s.as_bytes() {
148-
if c == b'|' {
149-
flag=!flag;
150-
} else if c == b'*'&&flag {
151-
ans+= 1;
145+
if c == b'*' {
146+
ans+=ok
147+
} else if c == b'|' {
148+
ok^= 1
152149
}
153150
}
154151
ans
@@ -159,20 +156,38 @@ impl Solution {
159156
### **C**
160157

161158
```c
162-
int countAsterisks(char *s) {
159+
int countAsterisks(char * s){
163160
int ans = 0;
164-
int flag = 1;
161+
int ok = 1;
165162
for (int i = 0; s[i]; i++) {
166-
if (s[i] == '|') {
167-
flag = !flag;
168-
} else if (s[i] == '*' && flag) {
169-
ans++;
163+
if (s[i] == '*') {
164+
ans += ok;
165+
} else if (s[i] == '|') {
166+
ok ^= 1;
170167
}
171168
}
172169
return ans;
173170
}
174171
```
175172
173+
### **C#**
174+
175+
```cs
176+
public class Solution {
177+
public int CountAsterisks(string s) {
178+
int ans = 0, ok = 1;
179+
foreach (char c in s) {
180+
if (c == '*') {
181+
ans += ok;
182+
} else if (c == '|') {
183+
ok ^= 1;
184+
}
185+
}
186+
return ans;
187+
}
188+
}
189+
```
190+
176191
### **...**
177192

178193
```
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
int countAsterisks(char *s) {
1+
int countAsterisks(char *s){
22
int ans = 0;
3-
int flag = 1;
3+
int ok = 1;
44
for (int i = 0; s[i]; i++) {
5-
if (s[i] == '|') {
6-
flag= !flag;
7-
} else if (s[i] == '*'&&flag) {
8-
ans++;
5+
if (s[i] == '*') {
6+
ans+=ok;
7+
} else if (s[i] == '|') {
8+
ok ^= 1;
99
}
1010
}
1111
return ans;
12-
}
12+
}

‎solution/2300-2399/2315.Count Asterisks/Solution.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public:
33
int countAsterisks(string s) {
4-
int ans = 0, t = 0;
4+
int ans = 0, ok = 1;
55
for (char& c : s) {
6-
if (c == '|')
7-
t ^= 1;
8-
else if (c == '*')
9-
ans += t == 0;
6+
if (c == '*') {
7+
ans += ok;
8+
} else if (c == '|') {
9+
ok ^= 1;
10+
}
1011
}
1112
return ans;
1213
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /