题解 | #子数组最大乘积#

子数组最大乘积

http://www.nowcoder.com/practice/9c158345c867466293fc413cff570356

数组的子数组,前提是需要按照顺序排列,并不是元素的全排列。
有0周围的结果都是0嚒

class Solution:
    def maxProduct(self , arr ):
        res = max(arr)
        tmp = arr[0]
        # write code here
        for i in range(len(arr))://先计算有多少元素,就是他的范围
            tmp = arr[i]
            for j in range(i+1, len(arr))://在上面遍历个数的时候,在这个范围内做遍历乘积
                print(res,tmp, arr[i+1:j+i+1])//有错误,我输出了一下
                tmp *= arr[j]
                if tmp > res:
                    res = tmp

                    if tmp == 0:
                        tmp = 1
                    else:
                        tmp=tmp//这里要付给tmp,因为上面for还在做乘积,如果缺失,数组中有两个负数就不能乘积了,有的案例会通不过。
        return res
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务