LintCode-劍指Offer-(41)最大子數組

class Solution {
public:
    /**
    * @param nums: A list of integers
    * @return: A integer indicate the sum of max subarray
    */
    int maxSubArray(vector<int> nums) {
        // write your code here
        int summax = nums[0];
        int sumcur = 0;
        for (int i = 0;i<nums.size();i++){
            sumcur = getMax(i,nums);
            if (summax<sumcur)summax = sumcur;
        }
        return summax;
    }
    int getMax(int startloc,vector<int>& nums){
        int summax = nums[0];
        int sumcur = 0;
        for (int i = startloc;i<nums.size();i++){
            sumcur += nums[i];
            if (summax<sumcur)
                summax = sumcur;
        }
        return summax;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章