leetcode 53. Maximum Subarray(DP动态规划问题)
问题描述:
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动态规划问题)相关推荐
- 【DP】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...
- 【动态规划】LeetCode 53. Maximum Subarray
LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...
- [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray
41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...
- LeetCode(53):Maximum Subarray
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- leetCode 53. maximum subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]: 53: Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode#53 Maximum Subarray
Problem Difinition: Find the contiguous subarray within an array (containing at least one number) wh ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 53.Maximum Subarray
/** 53.Maximum Subarray * 2016-5-7 by Mingyang * 如果我们从头遍历这个数组.对于数组中的其中一个元素,它只有两个选择: 1.* 要么加入之前的数组加和之 ...
- LeetCode 53. Maximum Subarray--动态规划--C++,Python解法
题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...
最新文章
- Spring中的这些坑,99%的程序员都踩过
- 关于Eclipse平台的使用和开发第一个SWT程序
- linux显示父目录,linux – 仅列出文件的公共父目录
- [C++11]final关键字的使用
- [转载] Python中TFTP的理解
- OPPO Reno巴萨定制版正式开售 10倍混合光变加持专属定制
- 【Redis】redis-3.0.0安装以及集群的搭建
- 青龙面板出现需要409000秒才能登陆,加忘记密码
- HUD 2544 最短路 迪杰斯特拉算法
- vueCli3 身份证正则校验
- 动态背景 图层上写文字_文字效果很难吗?教你如何打造绚丽的浮雕文字!
- 6.wireshark使用全解
- docker理念:不可变基础设施
- Vue 富文本编辑器的使用
- Excel作统计图表
- 安装win10虚拟机遇到的坑
- 80004005错误代码_0x80004005,小编教你解决0x80004005错误代码的方法
- python使用多线程爬取
- [渝粤教育] 兴义民族师范学院 数据结构 参考 资料
- 【IoT】产品设计:拼多多商业模式分析:互联网是商业模式的竞技场
热门文章
- python 标准库: csv
- java毕业生设计药品管理系统演示录像2021计算机源码+系统+mysql+调试部署+lw
- DiskGenius 强行拆分黑苹果HFS硬盘分区以给Windows扩容
- OpenCV中Rect()函数常用操作和使用详解(含绘图示例)+Rectangle()函数用法
- 最长公共子串(动态规划)
- java 写文件换行_Java 写文件实现换行
- QGIS制图:矢量数据制图流程及导出
- ViewPager Kotlin 教程入门
- 高德地图API-获取位置信息的经纬度
- 基于区块链的食品安全溯源系统