美文网首页Leetcode
Leetcode 525. Contiguous Array

Leetcode 525. Contiguous Array

作者: SnailTyan | 来源:发表于2018-09-12 18:33 被阅读2次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Contiguous Array

    2. Solution

    class Solution {
    public:
        int findMaxLength(vector<int>& nums) {
            int length = 0;
            int sum = 0;
            unordered_map<int, int> m({{0, -1}});
            for(int i = 0; i < nums.size(); i++) {
                sum += nums[i]?1:-1;
                if(m.count(sum)) {
                    length = max(length, i - m[sum]);
                }
                else {
                    m[sum] = i;
                }
            }
            return length;
        }
    };
    

    Reference

    1. https://leetcode.com/problems/contiguous-array/description/

    相关文章

      网友评论

        本文标题:Leetcode 525. Contiguous Array

        本文链接:https://www.haomeiwen.com/subject/fhqbgftx.html