力扣周赛83vp

50min AK,状态一般。

A

记录起点,遇到不同就更新。

B

按题意模拟。

C

直接 \(O(\sqrt n)\) 暴力。

class Solution {
  public:
    int consecutiveNumbersSum(int n) {
        ll m = 2 * n + 1;
        ll x = sqrt(m);
        int res = 0;
        for (int i = 1; i <= x; ++i) {
            if (i & 1) {
                if (n % i)
                    continue;
                res++;
            } else {
                int mid = i / 2;
                if (n % mid || (n / mid) % 2 == 0)
                    continue;
                res++;
            }
        }
        return res;
    }
};

D

对每个字母分别求贡献,\(O(n)\)

class Solution {
  public:
    int uniqueLetterString(string s) {
        vector<int> pos[26];
        for (int c = 0; c < 26; ++c) {
            pos[c].push_back(-1);
        }
        for (int i = 0; i < s.length(); i++) {
            int now = s[i] - 'A';
            pos[now].push_back(i);
        }
        for (int c = 0; c < 26; ++c) {
            pos[c].push_back(s.length());
        }
        ll ans = 0;
        for (int c = 0; c < 26; ++c) {
            if (pos[c].size() <= 2)
                continue;
            for (int i = 1; i < pos[c].size() - 1; i++) {
                ans += 1LL * (pos[c][i] - pos[c][i - 1]) * (pos[c][i + 1] - pos[c][i]);
                ans %= mod;
            }
        }
        return ans % mod;
    }
};
posted @ 2021-10-01 14:16  Theophania  阅读(31)  评论(0)    收藏  举报