题解 | #乘积为正数的最长连续子数组#
乘积为正数的最长连续子数组
https://www.nowcoder.com/practice/0112b9b5a09048d89309f55ea666db91
#include <bits/stdc++.h> int main(int argc, char *argv[]) { int len, val, neg, pos, res; neg = pos = res = 0; std::cin >> len; for (int i = 0; i < len; ++i) { std::cin >> val; if (val > 0) { ++pos; neg = neg == 0 ? 0 : neg + 1; } else if (val < 0) { int tmp = neg; neg = pos + 1; pos = tmp == 0 ? 0 : tmp + 1; } else { neg = pos = 0; } res = std::max(res, pos); } std::cout << res << std::endl; return 0; }