题解 | #连续子数组的最大乘积#

连续子数组的最大乘积

https://www.nowcoder.com/practice/abbec6a3779940aab2cc564b22d36859

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */
    public int maxProduct (List<int> nums) {
        // write code here
        if (nums == null || nums.Count == 0)
            return 0;
        List<int> lsF = new List<int>() {
            nums[0]
        };
        List<int> ls = new List<int>() { };
        int nMaxMult = lsF[0];
        for (int i = 1; i < nums.Count; i++) {
            ls.Clear();
            ls.Add(nums[i]);
            nMaxMult = nMaxMult > nums[i] ? nMaxMult : nums[i];
            for (int j = 0; j < lsF.Count; j++) {
                int nVal = nums[i] * lsF[j];
                if (ls.IndexOf(nVal) < 0)
                    ls.Add(nVal);
                nMaxMult = nMaxMult > nVal ? nMaxMult : nVal;
            }
            lsF.Clear();
            lsF.AddRange(ls);
        }
        return nMaxMult;
    }
}

全部评论

相关推荐

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