LeetCode523(2021.6.2)

LC523. 连续的子数组和

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        int n = nums.size();
        vector<int> s(n + 1);
        unordered_map<int, int> res;
        for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + nums[i - 1];
        for (int i = 2; i <= n; ++i) {
            res[s[i - 2] % k] = 1;
            if (res[s[i] % k] == 1) return true;
        }
        return false;
    }
};

发表评论