eric_yam
eric_yam
采纳率14.3%
2020-03-02 17:45 阅读 139
已采纳

数据结构统计求大佬帮忙

大佬,一组数里面是0,0,0,1,0,0,2,2,0,2,2,0,0,3,0,0,0,0 (0,2,2,0,2,2就算一个室早三联)如果022022是连续出现的话也只算一个比如022022022022这样也只算一个不连续的022022才++,统计022022的个数,求大佬帮忙

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    weixin_41862148 懵懵的小哥 2020-03-02 18:04
    package com.lee.study;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Study20 {
        public static int getAmount(int[] numbers) {
    
            Pattern p = Pattern.compile("(?<!022022)022022(?!022022)");
    
            StringBuilder stringBuilder = new StringBuilder(numbers.length);
    
            for (int number : numbers) {
                stringBuilder.append(number);
            }
    
            Matcher m = p.matcher(stringBuilder.toString());
    
            int counter = 0;
    
            while (m.find()) {
                counter++;
            }
    
            return counter;
        }
    
        public static void main(String[] args) {
            int[] numbers = {0, 0, 0, 1, 0, 0, 2, 2, 0, 2, 2,0, 2, 2, 0, 2, 2, 0, 0, 3, 0, 0, 0, 0};
            System.out.println("应该输出1");
            System.out.println(getAmount(numbers));
    
        }
    }
    
    

    如果采纳,希望给个赞哦!@@

    点赞 评论 复制链接分享

相关推荐