2315. 统计星号 - 力扣(LeetCode)
一、Java
class Solution {
public int countAsterisks(String s) {
int cnt = 0;
boolean flag = true;
for(char c: s.toCharArray()) {
if(c == '|') flag = !flag;
else if(c == '*' && flag) cnt++;
}
return cnt;
}
}
该文章介绍了如何使用Java编写Solution类中的countAsterisks方法,计算给定字符串中连续的星号(*)字符的数量,遇到竖线(|)时切换计数状态。

被折叠的 条评论
为什么被折叠?



