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] = dp[i-1]+nums[i] ,dp[i-1]>0

dp[i] = nums[i] ,else

 class Solution {
public int maxSubArray(int[] nums) {
int dp[] = new int[nums.length+1];
int max = nums[0];
dp[0] = nums[0];
for(int i =1;i<nums.length;i++){
if(dp[i-1]<=0)
dp[i] = nums[i];
else
dp[i] = dp[i-1]+nums[i];
max = Math.max(dp[i],max);
}
return max;
}
}

53. Maximum Subarray(动态规划 求最大子数组)的更多相关文章

  1. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

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

  2. LeetCode 53. Maximum Subarray(最大的子数组)

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

  3. 53. Maximum Subarray(动态规划)

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

  4. [LeetCode] Maximum Product Subarray 求最大子数组乘积

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

  5. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  6. 刷题53. Maximum Subarray

    一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...

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

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

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

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

  9. Leetcode#53.Maximum Subarray(最大子序和)

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

随机推荐

  1. python2.0_s12_day15_django框架的基本使用

    day15本节内容介绍 上节作业讲解(让行进入编辑模式,批量编辑) CSS之特殊内容补充 CSS内容补充之伪类 伪类实例:返回顶部终极版 CSS内容补充之无法被覆盖 jQuery插件 jQuery插件 ...

  2. ping命令和telnet命令

    1.检查能不能连接上远程主机 ping  主机ip 2.检查远程主机端口是不是开放 telnet 198.10.10.69 1521 Trying 198.10.10.69...Connected t ...

  3. Zookeeper(三)-- JAVA原生API

    一.前提 jar包:zookeeper-3.4.9.jar,slf4j-api-1.6.1.jar,slf4j-log4j12-1.6.1.jar,log4j-1.2.15.jar 二.Demo pa ...

  4. Linux mysql 命令

    mysql 是 MySQL 服务的一个命令行工具,常见用法如下: [root@localhost ~]$ mysql -uroot -p' # 本地连接 MySQL 服务 [root@localhos ...

  5. Unity中Oculus分屏相机和普通相机一键切换

    Unity中Oculus分屏相机和普通相机一键切换 一.OCulus 分屏相机介绍 在VR开发工程中,总会觉得OC分屏的处理太慢,严重浪费时间啊! 但是不使用有不好调试,来回切换相机就成为了一个必须. ...

  6. MQTT--单片机实现即时通信

    链接--->http://blog.csdn.net/zhou6343178/article/details/51182623 1.准备条件: a)硬件:ATMEGA644PA 8位单片机 EN ...

  7. source insight 如何建工程--以及快捷方式查找调用函数方法

    在source insight的view菜单中点出relation window,然后右键点relation window,选relation window properties,然后把view re ...

  8. MQTT 3.1协议非严肃反思录

    前言 MQTT 3.1协议在弱网络环境下(比如2G/3G等)表现不够好,因此才有了反思. 弱网环境下表现 手机等终端在弱网络环境下丢包情况会非常明显,连接MQTT Server成功率很低.相比单纯的请 ...

  9. <a>标签实现链接和锚点的区别

    如果是实现链接,a标签中必须有href属性,并且属性值是合法的url 如果实现锚点,a标签中必须有name属性,当点击该标签时,会跳转到id同该标签的name值相同的元素处.

  10. 深入理解javascript原型和闭包 摘要

    一切(引用类型)都是对象,对象是属性的集合 对象都是通过函数创建的 隐式原型 Instanceof的判断队则是:沿着A的__proto__这条线来找,同时沿着B的prototype这条线来找,如果两条 ...