博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 53: Maximum Subarray
阅读量:4583 次
发布时间:2019-06-09

本文共 736 字,大约阅读时间需要 2 分钟。

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.

 

1 public class Solution { 2     public int MaxSubArray(int[] nums) { 3         int curSum = 0, result = Int32.MinValue; 4          5         int j = 0; 6         while (j < nums.Length) 7         { 8             curSum += nums[j]; 9             result = Math.Max(result, curSum);10             if (curSum < 0)11             {12                 curSum = 0;13             }14             15             j++;16         }17         18         return result;19     }20 }

 

转载于:https://www.cnblogs.com/liangmou/p/7807559.html

你可能感兴趣的文章
SQL Server 存储图片
查看>>
php特级课---4、网站服务监控(常用网站服务监控软件有哪些)
查看>>
ubuntu14.04 boost 1.58.0 安裝
查看>>
漏洞基本概念
查看>>
直角三角形 (Standard IO)
查看>>
web 12
查看>>
Centos7安装Nginx
查看>>
探讨在线支付平台支付接口的设计
查看>>
【设计模式】常用设计模式总结
查看>>
.NET中的六个重要概念
查看>>
二十九、简谈设计模式
查看>>
js中数组的检测方法
查看>>
[译]GotW #6a: Const-Correctness, Part 1
查看>>
JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)...
查看>>
用Python学分析 - 单因素方差分析
查看>>
2018个人年终总结
查看>>
[编辑排版]小技巧---markdown 转 richText
查看>>
JSON_UNESCAPED_UNICODE
查看>>
bug解决思路
查看>>
Oracle没有WM_CONCAT函数的解决办法
查看>>