首页 > 7.22 阿里 第二题 没参加笔试 自己瞎写的
头像
hkyoung
编辑于 2020-07-23 15:33
+ 关注

7.22 阿里 第二题 没参加笔试 自己瞎写的

原题:
    长度为 n 的数组,数组中每个元素 a 满足:1<=a<=n
    求连续区间的数量,要求区间中相同元素的数量 >=m
方法:双指针
#include<iostream>
using namespace std;

void main() {
    int a[] = { 1,2,1,2,5 };
    int n = 5, m = 2;
    int l = 0;
    int count = 0;
    while (l < n) {
        int r = l;
        int cnt = 0;
        while (r < n) {
            if (a[r] == a[l]) ++cnt;
            if (cnt == m) {
                count += n - r;
                break;
            }
            else {
                ++r;
            }
        }
        ++l;
    }
    cout << count << endl;
}


全部评论

(0) 回帖
加载中...
话题 回帖

相关热帖

近期精华帖

热门推荐