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 ...
随机推荐
- Javascript进阶篇——浏览器对象—Location、Navigator、userAgent、screen对象
Location对象location用于获取或设置窗体的URL,并且可以用于解析URL.语法: location.[属性|方法] location对象属性图示: location 对象属性: loca ...
- Android零碎知识点总结
1 简单的跨进程通信可以用Messenger类,不用AIDL. 2 当一个Service没有action时,它默认是exported="false"的,其它进程用它的包名和类名构造 ...
- jxl读和取Excel文件
请参看下面链接: jxl如何读和取excle中的数据
- Unity3d Web Player 的server端联网配置
新游戏出了第一个能跑完流程的版本,不得不佩服Unity3D强大的功力,PC.MAC OS.Linux.IOS.Android.web player,前天刚发布的unity3d 4.2版本还支持WIND ...
- transient 做个标记
import java.io.*; import java.util.*; public class Logon implements Serializable { /** * */ private ...
- Python修改文件名
Python批量修改文件名 # -*- coding: cp936 -*- import os from nt import chdir path="./files/" froms ...
- LINUX系统GIT使用教程
Git使用笔记. 1 安装GIT $ sudo aptitude install git $ sudo aptitude install git-doc git-svn git-email git ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- LeetCode_Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- 多线程实际运用<第七篇>
1.单线程采集100个页面 class Program { static int i = 6991275; static void Main(string[] args) { Stopwatch sw ...