Maximum Subarray (JAVA)
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.
public static int maxSubArray(int[] A)
{
int max=A[0];
for(int i=0;i<A.length;i++)
{
int sum=0;
for(int j=i;j<A.length;j++)
{
sum+=A[j];
if(sum>max) max=sum; }
} return max;
}
O(n):
public static int maxSubArray(int[] A)
{
int max=A[0];
int sum=0;
for(int i=0;i<A.length;i++)
{
sum+=A[i];
if(sum>max)
max=sum; if(sum<0)
sum=0;
}
return max;
}
Maximum Subarray (JAVA)的更多相关文章
- 53. Maximum Subarray (JAVA)
iven an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
随机推荐
- js数组去重,并统计最多项算法
从事了一段时间的前端开发,今天写了一个数组去重,并统计最多项的方法,目前只支持数组的项都是数字. 由于本人能力有限,希望能得到网友的指正!如有问题或者更好的实现思路,也欢迎大家和我讨论!代码如下: f ...
- python re 正则
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- UI开发学习中遇到的问题汇总
1.给UIView设置圆角,边框,阴影绘制,需要使用layer 1)设置圆角cornerView.layer.cornerRadius = 20; //设置试图圆角的大小cornerView.laye ...
- java基础学习笔记
Q: What if the main method is declared as private? 如果将主函数声明为私有类型的会怎样? A: The program compile ...
- OpenCV——Mat,IplImage,CvMat类型转换
Mat,cvMat和IplImage这三种类型都可以代表和显示图像,三者区别如下 Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化. 而CvMat和IplImage类型更侧 ...
- Android SDK 更新时修改hosts文件仍然无法更新,可试试这个方法……
Android SDK 更新时修改hosts文件仍然无法更新,此时必定万分蛋疼.在hosts文件中更换了各种ip,仍然解决不了!!!!!!!!!!!!!!? 第一步: 打开此软件,等待服务器连接 第二 ...
- SQL语句 计算某段时间工作日的天数(除了周六日)
--只是加了固定日期,可以根据需求给成变量形式(BY 少年工藤) -思路:根据日期区间循环判断每一天是周日(1).周六(7)不变,其他加1 1 DECLARE @DAY DATE,@COUNT INT ...
- 在Docker上部署使用Azure CLI镜像
Docker是非常流行的容器技术,在Docker中安装部署多种工具非常快速和方便:而Azure CLI是微软提供的可以在Linux/Mac上运行的跨平台命令行管理工具,本文介绍如何在Azure上安装部 ...
- 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)
最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题, 用 echo phpinfo() ; 查看, 支持op ...
- 多线程实际运用<第七篇>
1.单线程采集100个页面 class Program { static int i = 6991275; static void Main(string[] args) { Stopwatch sw ...