../ Competitive Programming
-rw-r--r--aguang621 B1 min read

Leet Code Daily 2024-06-08 - Continuous Subarray Sum

Solution

Preffix sum & hash.

Code

c++
class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        vector<int> sums = {0};
        for (const auto& x : nums) {
            sums.push_back(sums.back() + x);
        }
        unordered_set<int> mods;
        for (int i = 0; i < sums.size(); i++) {
            if (i - 2 >= 0) {
                mods.insert(sums[i - 2] % k);
            }
            if (mods.contains(sums[i] % k)) {
                return true;
            }
        }
        return false;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window