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

详见:https://leetcode.com/problems/maximum-subarray/description/

Java实现:

方法一:暴力解

class Solution {
public int maxSubArray(int[] nums) {
int size=nums.length;
if(size==0||nums==null){
return Integer.MIN_VALUE;
}
int maxSum=Integer.MIN_VALUE;
for(int i=0;i<size;++i){
int curSum=0;
for(int j=i;j<size;++j){
curSum+=nums[j];
if(curSum>maxSum){
maxSum=curSum;
}
}
}
return maxSum;
}
}

方法二:

class Solution {
public int maxSubArray(int[] nums) {
int size=nums.length;
if(size==0||nums==null){
return Integer.MIN_VALUE;
}
int maxSum=nums[0];
int curSum=nums[0];
for(int i=1;i<size;++i){
if(curSum<0){
curSum=nums[i];
}else{
curSum+=nums[i];
}
if(curSum>maxSum){
maxSum=curSum;
}
}
return maxSum;
}
}

053 Maximum Subarray 最大子序和的更多相关文章

  1. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  2. 53. Maximum Subarray最大子序和

    网址:https://leetcode.com/problems/maximum-subarray/submissions/ 很简单的动态规划 我们可以把 dp[i] 表示为index为 i 的位置上 ...

  3. LeetCode 53. Maximum Subarray最大子序和 (C++)

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

  4. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  5. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  6. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  7. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. [LeetCode] 53. Maximum Subarray 最大子数组

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

  9. Java for LeetCode 053 Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. listen 71

    Creepy People Leave You Cold Jack Nicholson, playing the crazed caretaker in The Shining, makes me r ...

  2. BZOJ1067&P2471 [SCOI2007]降雨量[线段树裸题+细节注意]

    dlntqlwsl 很裸的一道线段树题,被硬生生刷成了紫题..可能因为细节问题吧,我也栽了一次WA50分.不过这个隐藏条件真的对本菜鸡来说不易发现啊. 未知的年份连续的就看成一个就好了,把年份都离散化 ...

  3. Xshell 主机和远程机之间的文件传输

    (1)宿主机传输文件到远程机 方法1:直接拖动文件至xshell远程机命令行界面 方法2:远程机命令行输入rz打开文件选择框 (2)远程机传输文件到宿主机 远程机命令行界面上输入sz xxx.txt( ...

  4. MySQL安装过程中对The error code is 2203的解决方案

    MySQL安装过程中对The error code is 2203的解决方案 1.问题描述 Windows系统安装MySQL遇到The error code is 2203.,具体描述如下 The i ...

  5. ZigBee简介

    前言 目前,中国大力推广的物联网是zigbee 应用的主战场,物联网通过智能感知.识别技术与普适计算(我还特意申请了个域名psjs.vip).泛在网络的融合应用,被称为继计算机.互联网之后世界信息产业 ...

  6. Day04:函数参数、对象、嵌套、闭包函数和装饰器

    上节课复习:    1.什么是函数        函数就是具备某一功能的工具    2.为何用函数        1.程序的组织结构和可读性        2.减少代码冗余        3.扩展性强 ...

  7. 【win10激活问题】 从【win10专业工作站版】转为 数字许可证的【win10专业版】

    今天安装了 win10 1903 (10.0.18362 暂缺 Build 18362) 安装时 选的 是[win10 专业工作站版] 却无法激活, (因为当初是从win7升级上win10的,只有关联 ...

  8. 转载:SharePoint 2010 自定义 字段 类型--------省市区联动

    最近有几个朋友问到了有关自定义字段类型的问题,为了让更多的人了解自定义字段类型的方法,特写一篇博客与大家分享,首先看一下解决方案目录 创建自定义类型分以下几个步骤: 第一步:添加SharePoint映 ...

  9. SQL 排序规则 CodeProject

    http://www.cnblogs.com/ifreesoft/p/4259626.html 开发ERP数据维护工具之一 修改SQL Server数据库排序规则 Change Collation   ...

  10. PCL推荐的命名规范(2)

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=210 函数/成员函数命名 函数和类的成员函数的命名应该采用camelCase ...