自己想得太复杂了,网上查了别人的代码。。太简单了。。

 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 sum = 0;
 7         int ret = INT_MIN;
 8         for(int i = 0 ; i < n; ++i) {
 9             sum += A[i];
10             ret = max(ret, sum);
11             sum = max(sum, 0);
12         }
13         return ret;
14     }
15 };

C#

 1 public class Solution {
 2     public int MaxSubArray(int[] nums) {
 3         int sum = 0;
 4         int ans = Int32.MinValue;
 5         for (int i = 0; i < nums.Length; i++) {
 6             sum += nums[i];
 7             ans = Math.Max(ans, sum);
 8             sum = Math.Max(sum, 0);
 9         }
10         return ans;
11     }
12 }

View Code

转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/04/08/3007134.html

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 largest ...

  5. LeetCode - Maximum Subarray

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

  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. CVPR 2019收录论文ID公开,你上榜了吗?
  2. 求出歌手的得分python_哪位大侠帮我做做python的题目啊,做其中5个就好。跪求啊...
  3. 脚手架工程:介绍安装
  4. Blazor WebAssembly 应用程序中进行 HTTP 请求
  5. 嵌入式Linux系统编程学习之六make工程管理和Makefile
  6. Q85:对比“直接光照”和“间接光照”的反射模型
  7. 外包征集令:一个Android TV酒店项目
  8. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_22-CMS前端页面查询开发-Api调用-跨域解决...
  9. IIS发布web网站
  10. word宏加密的破解
  11. python 创建nc文件,包含经纬度
  12. VMware虚拟机磁盘扩容
  13. typecho插件,typechoSEO插件,typecho程序插件
  14. stata陈强:计量经济学及stata应用_陈强 第五章 多元线性回归
  15. NGINX反向代理缓存
  16. 二进制和文本文件的比较
  17. Redis源码精炼版
  18. ThreadLocal.withInitial
  19. SAP概念之利润中心(Profit Center)
  20. rm: cannot remove `/usr/local/tmp/‘: Directory not empty

热门文章

  1. linux 词霸,Linux中的词霸
  2. 父母有退休工资和没有退休工资的比较,家庭生活区别大吗?
  3. 三个火一个木组成的燊怎么读,燊是什么意思?
  4. 2020年对我影响最深的观点是下面3个,你呢?
  5. 微商人赚钱的4个关键点
  6. 年轻人必须在北上广工作吗
  7. 最近为A公司提炼的经营理念之合作理念
  8. 我发现有的人就活该他赚钱,他们的脑子就是个聚宝盆
  9. A卡比N卡画质好,真有此事吗?
  10. 麒麟810相当于骁龙多少?