C
c++ leetcode 525 contiguous array solution
데브웅
2021. 7. 24. 00:49
code >>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Solution {
public:
int findMaxLength(vector<int>& nums) {
const int numSize = (int)nums.size();
unordered_map<int, vector<int>> hashMap;
int result = 0;
hashMap[0].emplace_back(-1);
int accum = 0;
for (int i = 0; i < numSize; i++)
{
if (nums[i] == 0)
{
accum = accum + -1;
hashMap[accum].push_back(i);
}
else
{
accum = accum + 1;
hashMap[accum].push_back(i);
}
}
for (const auto& ind_hash : hashMap)
{
int diff = ind_hash.second.back() - ind_hash.second.front();
if (diff > result)
result = diff;
}
return result;
}
};
|