当前位置: 代码迷 >> 综合 >> Leetcode 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target用hashmap去做
  详细解决方案

Leetcode 1546. Maximum Number of Non-Overlapping Subarrays With Sum Equals Target用hashmap去做

热度:18   发布时间:2024-02-08 23:32:29.0
  • 对比 560
class Solution {
public:int maxNonOverlapping(vector<int>& nums, int target) {unordered_map<int,int> map;map[0]=1;int cnt=0;int sum=0;for(auto& x: nums){sum+=x;if(map.count(sum-target)){cnt++;map.clear();map[0]=1;sum=0;}else{map[sum]++;}}return cnt;}
};
  相关解决方案