1 题目:

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.

click to show more practice.

Hide Tags

Divide and Conquer Array Dynamic Programming

 
2 分析:
要找最大连续的子数组。好吧,动态规划题,想了半天,想不出递推式怎么写,可能与做的太少有关。只分析出求F(n)的话,有两种情况,一种是F(n-1)最大子串里面有A[n-1],一种没有,然后我就不知道怎么写了。。。看了看别人的代码,分析了一下,定义一个curSum,取肯定有A[i]的最大子串,然后比较maxSum与curSum就行了。
标记函数可以将
 maxSum = Math.max(maxSum, curSum);
改为if语句,存开始序号和子串长度。
 
3 代码:
    public int maxSubArray(int[] A){
if (A.length == 0) {
return 0;
}
int maxSum = A[0];
int curSum = A[0];
int len = A.length;
for (int i = 1; i < len; i++) {
curSum = Math.max(curSum + A[i], A[i]);
maxSum = Math.max(maxSum, curSum);
}
return maxSum;
}

letcode code]Maximum Subarray的更多相关文章

  1. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  2. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

  3. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  4. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  5. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

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

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

  7. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  8. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  9. LEETCODE —— Maximum Subarray [一维DP]

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

随机推荐

  1. 关于python的字符编码

    理论特别多,金角大王讲的非常细致和深入浅出. 我来个简短的总结: python2的编码:默认是ascii,可以改变成gbk,utf-8等,但是用什么编码写的,就存储成什么编码.如果搬到linux,默认 ...

  2. 使用Loadrunner对IBM MQ进行性能测试

    一.概述         使用Loadrunner对IBM MQ进行性能测试,需要用到java vuser以及java编码知识.此次先介绍什么是IBM MQ,然后java vuser的使用与配置细节, ...

  3. NC 6系初始化EJB

    6系开发时,调用远程接口去操作数据时,需先调用EJB. InvocationInfoProxy.getInstance().setUserDataSource(design); InvocationI ...

  4. Hive 系列(二)权限管理

    Hive 系列(二)权限管理 一.关于 Hive Beeline 问题 启动 hiveserver2 服务,启动 beeline -u jdbc:hive2:// 正常 ,启动 beeline -u ...

  5. 最新Dashboard设计实例、技巧和资源集锦,视觉和功能两不误,妥妥的!

    Dashboard设计,尽管设计师们叫法各不相同(例如:“数据面板设计”, “控制面板设计”, “仪表盘设计”或“后台界面设计”等等).但,此类设计的最终目都是力求以最直观.最简洁的方式呈现各种信息和 ...

  6. MarkDown,写出个性、漂亮的文档

    http://www.markdown.cn # Title1## Title2### Title3content==content2--content3--* name- name+ name * ...

  7. Oracal 学习之用户角色创建分配表空间 给角色分配权限

    //创建角色inspur 密码为inspur,默认的表空间为USERS create user inspur identified by inspur default tablespace USERS ...

  8. Windows-universal-samples学习笔记系列四:Data

    Data Blobs Compression Content indexer Form validation (HTML) IndexedDB Logging Serializing and dese ...

  9. python学习之ansible api

    Python API 2.0 从2.0的事情开始更复杂一些,但是你会得到更多离散和可读的类: #!/usr/bin/env python import json from collections im ...

  10. Java基础之多线程没那么复杂!

    多线程的引入 1.什么是多线程 线程是程序执行的一条路径,一个进程中可以包含多条线程;多线程并发执行可以提高程序的效率</br> 2.进程和线程之间的关系 操作系统可以同时执行多个任务,每 ...