[力扣] 算法 525 (C++)

525. 连续数组

class Solution {
	static inline int f(int x)
	{
		return 2 * x - 1;
	}
public:
	int findMaxLength(vector<int>&amp; nums) {
		unordered_map<int, size_t> map;
		nums[0] = f(nums[0]);
		map.emplace(nums[0], 0);
		size_t ret = 0;
		for (size_t i = 1, size = nums.size(), tmp; i < size; ++i)
		{
			nums[i] = nums[i - 1] + f(nums[i]);
			if (nums[i] == 0 &amp;&amp; (tmp = i + 1) > ret)
			{
				ret = tmp;
			}
			else
			{
				auto found = map.find(nums[i]);
				if (found == map.end())
				{
					map.emplace(nums[i], i);
				}
				else if ((tmp = i - found->second) > ret)
				{
					ret = tmp;
				}
			}
		}
		return ret;
	}
};