Maximum Subarray(最大子数组)
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.
class Solution {
public int maxSubArray(int[] nums) {
if(nums==null||nums.length==0) return 0;
int maxSum=nums[0];//最大和。初始值要为第一项
int theSum=nums[0];//当前项为止的最大和。初始值要为第一项,不要为0
//如果当前项的最大和小于等于0,那么theSum应该要从该项后面开始重新计算和,因为负数对和没有正帮助。
for(int i=1;i<nums.length;i++){
if(theSum>0)
theSum=theSum+nums[i];
else
theSum=nums[i];//初始值不为零,则这里初始化时也就不是0,
if(maxSum<theSum)
maxSum=theSum;
}
return maxSum;
}
}
初始为0,若只有一个元素,且为负数,此时就会输出0.不对
Maximum Subarray(最大子数组)的更多相关文章
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【LeetCode每天一题】Maximum Subarray(最大子数组)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [Leetcode] maximun subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 53. Maximum Subarray最大子序和
网址:https://leetcode.com/problems/maximum-subarray/submissions/ 很简单的动态规划 我们可以把 dp[i] 表示为index为 i 的位置上 ...
随机推荐
- Android开发中StackOverflowError
Android开发中StackOverflowError错误实例分析 一.概述 我在一个复杂的layout嵌套较多的android界面,碰到了java.lang.StackOverflowError这 ...
- 07 设置View的显示与隐藏
在代码中: 例子: <span style="font-size:14px;">ImageButton imageButton = new ImageButton(th ...
- ajax核心技术1---XMLHttpRequset对象的使用
AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.AJAX = 异步 Ja ...
- EBS DBA指南笔记(二)
第三章 监控和诊断 本章涵盖以下几个主题:监测的方法,数据库的监测,apache的监测,forms的监测,并发管理器的监测,服务器的监测,网络的监测,其它的一些监测和诊断方法. 1.监测的方法:主 ...
- 【翻译】Ext JS 6早期访问版本发布
早期访问版本是什么 如何参与 都包括什么 Sencha Ext JS 6 Sencha Pivot Grid Sencha Cmd 6 JetBrains IDE插件 反馈 原文:Announcing ...
- 一个简易版本的lua debugger实现
introduction 工欲善其事,必先利其器.lua作为一门动态语言,虽然我已经习惯了使用print来进行代码调试,但是还是有很多童鞋觉得一款好用的调试器能更好的进行lua代码编写.所以在以前接手 ...
- DesignModeler GestureRecgin…
DesignModeler : 设计模式 GestureRecginzer:手势识别 作者:韩俊强 原创版权地址:http://blog.sina.com.cn/s/blog_814ecfa9 ...
- 安装nodejs中遇到的问题
make clean 出现时间不对的问题的时候,主要是系统时间设置错误. 用date -s 来分别设置日期和时间 用clock -w来写入CMS 安装g++ yum -y install gcc-c+ ...
- ffdshow 源代码分析 3: 位图覆盖滤镜(设置部分Settings)
===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...
- GraphCuts算法解析,Graphcuts算法求最大流,最小割实例
图割论文大合集下载: http://download.csdn.net/detail/wangyaninglm/8292305 代码: /* graph.h */ /* Vladimir Kolmog ...