Maximum Subarray——LeetCode
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.
题目大意:给定一个数组,求最大连续子数组和。
解题思路:DP问题,首先分析一下数组,有正有负,那么可以假设第i个元素对应的最大连续子数组和是F[i],那么写出递推公式
F[i]=max{F[i-1]+num[i],num[i]}
就是说第i个元素最大连续子数组和是前i-1个最大连续子数组加上自己与自己相比,较大的那个;如果F[i-1]<0,那么F[i]=num[i],否则F[i]=F[i-1]+num[i],同时记录一个全局的最大值来更新。
public int maxSubArray(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
int max = nums[0];
int res = nums[0];
for(int i=1;i<nums.length;i++){
max=Math.max(nums[i]+max,nums[i]);
res = Math.max(res,max);
}
return res;
}
Maximum Subarray——LeetCode的更多相关文章
- Maximum Subarray - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Maximum Subarray - LeetCode 注意点 最大值有可能是正负数交替着出现 解法 解法一:一次遍历即可.当sum小于0的时候就重新开始 ...
- Maximum Subarray leetcode java
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
随机推荐
- sql根据'/'截取最后的字符串
filpath字段值:/DataFile/UpLoad/Logo/NoPhoto.jpg select filpath,REVERSE((SUBSTRING(REVERSE(FilPath),0,CH ...
- Android开发常见错误类型一览表
这是我的第一个博客,我会一直添加我在Android开发中遇到的错误,用来记录我开发中踩过的那些坑 ------------------------分割线------------------------ ...
- UTF-8和GBK有什么区别?
字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24位(三个字节)来编码.对于英文字符 ...
- android查看真机中的数据库
0.在有网的前提下1.安装 Android Studio,Lantern,Chrome浏览器2.在在githab上搜索stetho,打开第一个facebook/stetho3.在Gradle Scri ...
- ios开发之xcode6中如何添加pch全局引用文件
xcode6中去掉了默认添加pch文件,这就需要我们自己手动添加pch文件了,添加pch文件是为了一些琐碎的头文件引用,加快编译速度! 下面就说下该如何手动添加pch文件: 1.添加一个文件,在oth ...
- 通用方法解决dedecms导航调用二级、三级栏目菜单
博客之前做网站的时候经常会遇到二级菜单.三级菜单.了解dede的人都知道从5.5版本开始都有二级菜单的调用方法了,网上也有不少的教程文章.不过这个调用需要修改dede源码的二级菜单样式.个人感觉不是很 ...
- 提供进销存、ERP系统快速开发框架源码 (C#+SQL)
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...
- 如何正确的使用uwsgi
简单的安装过程可以在这里找到,这里主要说一下如何配置uwsgi的服务,将uwsgi服务加入系统进程,你可以使用如下两种方式安装 apt-get apt-get install uwsgi 该命令会自动 ...
- 张江在线APP演示
app下载地址:https://itunes.apple.com/cn/app/zhang-jiang-zai-xian/id722630317?mt=8
- Java中的自动拆箱装箱(Autoboxing&Unboxing)
一.基本类型打包器 1.基本类型:long.int.double.float.boolean 2.类类型:Long.Integer.Double.Float.Boolean 区别:基本类型效率更高,类 ...