• [LeetCode] 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.

    More practice:

    If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

    经典DP。二分版,过几日放上。
     1 class Solution {
     2 public:
     3     int maxSubArray(int A[], int n) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int ret = INT_MIN;
     7         int sum = 0;
     8         for(int i = 0; i < n; i++)
     9         {
    10             sum = max(sum + A[i], A[i]);
    11             ret = max(sum, ret);
    12         }
    13         
    14         return ret;
    15     }
    16 };

     

  • 相关阅读:
    String 方法
    异常处理
    数组长度改变方法
    对象
    重载(函数)
    函数
    java基础(死循环退出选项)
    cookie的封装,获取,删除
    事件监听的理解
    JS少数兼容
  • 原文地址:https://www.cnblogs.com/chkkch/p/2766586.html
Copyright © 2020-2023  润新知