【每日一题Day102】LC2315统计星号 | 模拟

统计星号【LC2315】

You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth.

Return the number of '*' in s, excluding the '*' between each pair of '|'.

Note that each '|' will belong to exactly one pair.

带侄子去医院 又错过周赛了 哎

  • 思路:记录每个'|'的编号,统计第偶数个'|'之后、第奇数个'|'之前的星号,以及末尾剩余字符串的星号个数

  • 实现

    class Solution {
        public int countAsterisks(String s) {
            int ans = 0;
            int count = 0;
            int idx = 0;
            for (char c : s.toCharArray()){
                if (c == '*'){
                    count++;
                }else if(c == '|'){
                    idx++;
                    if (idx % 2 == 1){
                        ans += count;                    
                    }
                    count = 0;
                }
            }
            return ans + count;
    
        }
    }
    
    • 复杂度
      • 时间复杂度: O ( n ) O(n) O(n) n n n为字符串的长度
      • 空间复杂度: O ( 1 ) O(1) O(1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值