首页 > 代码库 > LeetCode: Maximum Product Subarray

LeetCode: Maximum Product Subarray

很久没练只能看别人代码了

 1 class Solution { 2 public: 3     int maxProduct(int A[], int n) { 4         if (n == 0) return 0; 5         int curMax, curMin, ans; 6         ans = curMax = curMin = A[0]; 7         for (int i = 1; i < n; ++i) { 8             int tmp = curMin*A[i]; 9             curMin = min(A[i], min(tmp, curMax*A[i]));10             curMax = max(A[i], max(tmp, curMax*A[i]));11             ans = max(ans, curMax);12         }13         return ans;14     }15 };

 

LeetCode: Maximum Product Subarray