iven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

法I: 动态规划法

class Solution {
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE; //注意有负数的时候,不能初始化为0
int currentSum = Integer.MIN_VALUE;
for(int i = 0; i < nums.length; i++){
if(currentSum < 0) currentSum = nums[i];
else currentSum += nums[i]; if(currentSum > maxSum) maxSum = currentSum;
}
return maxSum;
}
}

法II:分治法

class Solution {
public int maxSubArray(int[] nums) {
return partialMax(nums,0,nums.length-1);
} public int partialMax(int[] nums, int start, int end){
if(start == end) return nums[start]; int mid = start + ((end-start) >> 1);
int leftMax = partialMax(nums,start, mid);
int rightMax = partialMax(nums,mid+1,end);
int maxSum = Math.max(leftMax,rightMax); int lMidMax = Integer.MIN_VALUE;
int rMidMax = Integer.MIN_VALUE;
int current = 0;
for(int i = mid; i >= start; i--){
current += nums[i];
if(current > lMidMax) lMidMax = current;
}
current = 0;
for(int i = mid+1; i <= end; i++){
current += nums[i];
if(current > rMidMax) rMidMax = current;
}
if(lMidMax > 0 && rMidMax > 0) maxSum = Math.max(lMidMax + rMidMax,maxSum);
else if(lMidMax > rMidMax) maxSum = Math.max(lMidMax,maxSum);
else maxSum = Math.max(rMidMax,maxSum);
return maxSum;
}
}

53. Maximum Subarray (JAVA)的更多相关文章

  1. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  2. 41. leetcode 53. Maximum Subarray

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

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  5. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  6. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  7. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  8. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  9. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

随机推荐

  1. python3:csv的读写

    前言快要毕业那会儿,在下编写了一个招聘网站招聘岗位的爬虫提供给前女神参考,最开始我是存到mysql中,然后在到处一份csv文件给前女神.到了参加工作后,由于经常使用excel绘制图表(谁叫公司做报表全 ...

  2. Python3学习笔记(七):字典

    在python中,有一种通过名字来引用值的数据结构,这种类型的数据结构成为映射. 字典是Python中唯一内建的映射类型,具有以下特点: 字典中的值是无序的 值存在特定的键(key)下 键(key)可 ...

  3. sqli-lab(15)

    要考四级了 翻译过来就是 基于时间的单引号盲注 0X01盲注 的了解 https://www.cnblogs.com/ldhbetter/p/9201840.html 这里写的清清楚楚 A 先拆解长度 ...

  4. spring cloud:feign-hystrix

    producer 1. File-->new spring starter project 2.add dependency       <dependency> <group ...

  5. 图的普里姆(Prim)算法求最小生成树

    关于图的最小生成树算法------普里姆算法 首先我们先初始化一张图: 设置两个数据结构来分别代表我们需要存储的数据: lowcost[i]:表示以i为终点的边的最小权值,当lowcost[i]=0说 ...

  6. 关于vue给对象新增属性页面不会动态更新

    不知道大家有没有遇到过这个问题,当我们给data里边声明或者已经赋值过的对象或者数组,添加新的属性时,如果更新此属性的值是不会动态更新视图的. $set 看以下实例: 我们开始给drug_list追加 ...

  7. linux中 > 、>> 的用法

    linux中>表示覆盖原文件内容(文件的日期也会自动更新),>>表示追加内容(会另起一行,文件的日期也会自动更新). 1 将history命令执行的结果保存到history.log文 ...

  8. Linux驱动开发8——中断处理

    中断包括软中断和硬中断两种,中断是一种异步I/O机制,即中断可以发生在任意时间点. 1.硬中断 硬件中断包括触发中断和处理中断两部分,而维系两者的是中断号,中断号是一种硬件资源. 1.1.注册和释放中 ...

  9. SOUI3.0仿Android插值动画使用方法

    在Android系统中,有插值动画,数值动画,属性动画,帧动画. 帧动画,在SOUI里可以通过AnimateImg这个控件来实现,其它几种动画3.0之前不支持,需要类似动画效果,只能自己通过定时器去实 ...

  10. MAC安装navcat

    安装及破解,参照此文: https://blog.csdn.net/marswill/article/details/79808416