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 };

 

[LeetCode] Maximum Subarray相关推荐

  1. [LeetCode] Maximum Subarray 最大子数组

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

  2. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  3. LeetCode Maximum Subarray

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

  4. LeetCode - Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. LeetCode: Maximum Subarray

    自己想得太复杂了,网上查了别人的代码..太简单了.. 1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 // Sta ...

  6. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  7. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  8. [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray

    41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...

  9. LeetCode(53):Maximum Subarray

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

最新文章

  1. stat用法:获取文件对应权限的数字
  2. StackOverflow 上面最流行的 7 个 Java 问题!
  3. App.config/Web.config 中特殊字符的处理
  4. Angular module加载的原理研究
  5. 04-课后作业1-字符串加密
  6. java activit怎么画图_Activiti 快速入门指南(用Java编写服务任务)
  7. c++代码健壮性_复活Navex-使用图查询进行代码分析(上)
  8. 微信公众号服务器模式,微信公众平台的两种模式
  9. oracle 表的移动,oracle 表移动表空间
  10. 电脑html游戏,电脑网页游戏排行榜
  11. liunx服务器间拷贝文件,linux 不同服务器之间拷贝文件
  12. Scikit-learn:分类classification
  13. The host '192.168.174.130' is unreachable. the host may be down..............
  14. javascript无提示关闭窗口,兼容IE,Firefox
  15. spring教程笔记5
  16. fiddler4使用教程
  17. java嵌入式软件开发工程师_嵌入式软件工程师笔试题
  18. 【分享】地产集团公司LOGO设计
  19. 找工作时,如何区分对方是不是培训机构?
  20. 送书 |《Python数据分析从小白到专家》

热门文章

  1. 索尼发布全球首款内置AI传感器,1200万像素,660元起售
  2. 马斯克因何财报会议怒爆粗口?明明是史上最佳Q1业绩,股价还一度大涨10%
  3. 在任天堂Switch上倒贴30块钱“加班”,我一个程序员还觉得很刺激
  4. 华为云HiLens Kit上手初探:一款几乎“零门槛”的AI开发套件
  5. 阿里AI再揽2员大将:挖角高通、360,年薪百万美元
  6. 不忘初心,努力做最好的自己
  7. ecshop仿淘宝加入购物车弹出框【支持任何页面】淡出淡隐固定屏幕-兼容ie
  8. HDU-1128 Self Numbers 筛选
  9. 筑业和恒智天成哪个好_浙江新闻客户端:废弃物变身创意作品 天成小学垃圾分类工作有声有色...
  10. 写个测试程序测试下,单片机使用浮点型数效率有多低