diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/continuous_subarr_sum.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/continuous_subarr_sum.cpp')
-rw-r--r-- | other/continuous_subarr_sum.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/other/continuous_subarr_sum.cpp b/other/continuous_subarr_sum.cpp new file mode 100644 index 0000000..ceaf3ce --- /dev/null +++ b/other/continuous_subarr_sum.cpp @@ -0,0 +1,31 @@ +#include "leetcode.h" + +class Solution { +public: + bool checkSubarraySum(vector<int> &nums, int k) { + unordered_map<int, int> um; + int s = 0; + um[0] = -1; + for (int i = 0; i < nums.size(); i++) { + s += nums[i]; + if (k != 0) + s %= k; + if (um.find(s) != um.end()) { + if (i - um[s] > 1) + return true; + } else + um[s] = i; + } + return false; + } +}; + +int main() { + Solution obj; + vector<int> nums = {23, 2, 4, 6, 7}; + int k = 6; + if (obj.checkSubarraySum(nums, k)) + cout << "true"; + else + cout << "false"; +} |