53. Maximum Subarray (JAVA)
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)的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- pyinstaller打包的exe太大?你需要嵌入式python玄学 探索篇
上篇我们讲到pip的安装以及普通库用pip的安装方法 CodingDog:pyinstaller打包的exe太大?你需要嵌入式python玄学 拓展篇zhuanlan.zhihu.com 问题纷沓而 ...
- CentOS查看进程端口号以及kill操作
查看端口: 使用 netstat -anp | grep 8090即:netstat –apn | grep 8090 查看进程:1.ps 命令用于查看当前正在运行的进程,grep 是 ...
- [BZOJ1059]:[ZJOI2007]矩阵游戏(二分图匹配)
题目传送门 题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N×N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种 ...
- legend3---用Homestead配置后报错“No input file specified.”
legend3---用Homestead配置后报错“No input file specified.” 一.总结 一句话总结: 自己项目上传到github的时候多增加了一层legend3的github ...
- jpg图片转换为yuv
ffmpeg -i d:/demo.jpg -s 400x500 -pix_fmt yuvj420p d:/test.yuv ffmpeg -i http://www.test.com/test.fl ...
- React Native商城项目实战04 - 封装TabNavigator.Item的创建
1.Main.js /** * 主页面 */ import React, { Component } from 'react'; import { StyleSheet, Text, View, Im ...
- 为java类起别名
<typeAliases> <!-- 1.typeAlias:为某个java类型起别名 type:指定要起别名的类型全类名;默认别名就是类名小写:employee alias:指定新 ...
- SQLAlcvchem
一.安装(稳定版的1.2.17) 二.一般使用(切记切记不要使用模块的名字作为项目名字,否则会出现玄学解决不了的问题------坑) #.导入SQLALchemy from sqlalchemy.ex ...
- ES6 class 语法糖不能直接定义原型上的属性
今天注意到两个东西: 1.为了模拟面向对象,JavaScript的class语法糖屏蔽了原型的概念 class A{ a = 1 // 注意!!这里定义的不是在prototype上的属性,而是给实 ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_6 mybatis注解开发一对一的查询配置
新建Account实体类 生成getter和setter还有toString方法 先创建dao类 全局的配置,这里要改成package 创建多对一的关系 在查询的时候输出user这个对象的内容 建立查 ...