zoukankan      html  css  js  c++  java
  • [leetcode] 53. Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

    For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
    the contiguous subarray [4,-1,2,1] has the largest sum = 6


    只要知道 除非全负,任何的负的子序列不可能是最优子序列的前缀 就可以了。

    我的代码:

    class Solution {
    public:
        int maxSubArray(vector<int>& nums) {
            int max = nums[0], now = 0;
            for (int i = 0; i < nums.size(); i++) {
                now += nums[i];
                if (now >= max) max = now;
                if (now < 0) now = 0;
            }
            return max;
        }
    };
  • 相关阅读:
    父子组件例子
    指令
    使用git将代码推到coding
    vue生命周期
    vue 父子组件
    vuex
    使用vue与element组件
    vue02
    使用vue
    telnet 使用
  • 原文地址:https://www.cnblogs.com/zmj97/p/7767443.html
Copyright © 2011-2022 走看看