int longestSubarray(vector<int> &nums) {
vector<int> v;
for (int i = 0; i < nums.size(); i++)
if (nums[i] == 0)
v.emplace_back(i);
if (v.empty())
return nums.size() - 1;
int count = 0;
for (int i = 0; i < v.size(); i++) {
int idx1 = 0;
for (int j = v[i] - 1; j >= 0; j--)
if (nums[j] == 1)
idx1++;
else
break;
for (int j = v[i] + 1; j < nums.size(); j++)
if (nums[j] == 1)
idx1++;
else
break;
count = max(count, idx1);
}
return count;
}
5434. 删掉一个元素以后全为 1 的最长子数组
最新推荐文章于 2023-08-01 19:16:44 发布