问题描述:

  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) return 0;int sum = 0;int max = Integer.MIN_VALUE;for(int i = 0 ; i < nums.length; i++){if(sum + nums[i] > nums[i])sum += nums[i];elsesum = nums[i];if(sum > max)max = sum;}return max;       }
}

leetcode 53. Maximum Subarray(DP动态规划问题)相关推荐

  1. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  2. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  3. [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray

    41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...

  4. LeetCode(53):Maximum Subarray

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

  5. leetCode 53. maximum subarray

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

  6. [LeetCode]: 53: Maximum Subarray

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

  7. LeetCode#53 Maximum Subarray

    Problem Difinition: Find the contiguous subarray within an array (containing at least one number) wh ...

  8. C#解leetcode 53.Maximum Subarray

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

  9. 53.Maximum Subarray

    /** 53.Maximum Subarray * 2016-5-7 by Mingyang * 如果我们从头遍历这个数组.对于数组中的其中一个元素,它只有两个选择: 1.* 要么加入之前的数组加和之 ...

  10. LeetCode 53. Maximum Subarray--动态规划--C++,Python解法

    题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...

最新文章

  1. Spring中的这些坑,99%的程序员都踩过
  2. 关于Eclipse平台的使用和开发第一个SWT程序
  3. linux显示父目录,linux – 仅列出文件的公共父目录
  4. [C++11]final关键字的使用
  5. [转载] Python中TFTP的理解
  6. OPPO Reno巴萨定制版正式开售 10倍混合光变加持专属定制
  7. 【Redis】redis-3.0.0安装以及集群的搭建
  8. 青龙面板出现需要409000秒才能登陆,加忘记密码
  9. HUD 2544 最短路 迪杰斯特拉算法
  10. vueCli3 身份证正则校验
  11. 动态背景 图层上写文字_文字效果很难吗?教你如何打造绚丽的浮雕文字!
  12. 6.wireshark使用全解
  13. docker理念:不可变基础设施
  14. Vue 富文本编辑器的使用
  15. Excel作统计图表
  16. 安装win10虚拟机遇到的坑
  17. 80004005错误代码_0x80004005,小编教你解决0x80004005错误代码的方法
  18. python使用多线程爬取
  19. [渝粤教育] 兴义民族师范学院 数据结构 参考 资料
  20. 【IoT】产品设计:拼多多商业模式分析:互联网是商业模式的竞技场

热门文章

  1. python 标准库: csv
  2. java毕业生设计药品管理系统演示录像2021计算机源码+系统+mysql+调试部署+lw
  3. DiskGenius 强行拆分黑苹果HFS硬盘分区以给Windows扩容
  4. OpenCV中Rect()函数常用操作和使用详解(含绘图示例)+Rectangle()函数用法
  5. 最长公共子串(动态规划)
  6. java 写文件换行_Java 写文件实现换行
  7. QGIS制图:矢量数据制图流程及导出
  8. ViewPager Kotlin 教程入门
  9. 高德地图API-获取位置信息的经纬度
  10. 基于区块链的食品安全溯源系统