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

首先

當題目涉及到求最大最小值時,最初的比較數字就應當設置爲INT_MAX或INT_MIN,更爲安全。

<limits.h>中有INT_MAX和INT_MIN的宏定義可直接使用。

或者自行定義宏

#define INT_MAX 0x7fffffff

#define INT_MIN 0x80000000

INT_MAX = 

INT_MIN = -

然后

思路一:
进行两次循环,遍历所有可能的情况,找到最大的子数组,时间复杂度为O(n^2); 
思路二:
对于任意一个子数组和,如果大于0,那么它再添加一个数,他的贡献是正值,如果子数组和小于0,再添加一个数,它的贡献则为负值,这时候不如将当前子数组舍掉,新数成为一个新数组。(动态规划Dynamic Programming)

思路一

public int maxSubArray(int[] nums) {
// write your code
if (nums.length == ) return nums[];
int max = nums[];
for (int i = ; i < nums.length; i++){
int temp = nums[i];
for (int j = i+; j < nums.length; j++){ temp += nums[j];
if (temp > max){
max = temp;
}
}
} return max;
}

思路二

public int maxSubArray(int[] nums) {
// write your code
int max = Integer.MIN_VALUE;
int sum = Integer.MIN_VALUE;
for (int i = ; i < nums.length; i++){
sum = sum< ? nums[i] : nums[i]+sum;
if (sum>max){
max = sum;
}
} return max;
}

[LintCode笔记了解一下]41.Maximum Subarray的更多相关文章

  1. [LintCode笔记了解一下]44.Minimum Subarray

    这道题和max subarray很类似,我用local 和 global 的dp方式阔以解决这道 那么我们来看动态规划的四个要素分别是什么? State: localmin[i] 表示以当前第i个数最 ...

  2. 41. Maximum Subarray

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

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

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

  4. 41. leetcode 53. Maximum Subarray

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

  5. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  6. 【leetcode】Maximum Subarray (53)

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

  7. 算法:寻找maximum subarray

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

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

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

  9. 【leetcode】Maximum Subarray

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

随机推荐

  1. 升级phpcms的ckeditor编辑器

    首先说明一下为什么升级?网上很多人升级成了ueditor,可从fckedotror 到 ckeditor,我个人都是比较喜欢的,特别是开放式的插件方式.另外一个就是至少要懂得升级和插件的开发,这样也能 ...

  2. 02——微信小程序官方demo讲解——app部分

    第一节讲了目录结构,这节主要讲解下目录中app.js部分. 它由三部分组成app.js.app.json与app.wxss 1.JS部分 1.1概述 //app.js App({ onLaunch: ...

  3. NIO buffer 缓冲区 API

    package bhz.nio.test; import java.nio.IntBuffer; public class TestBuffer { public static void main(S ...

  4. iPhone开发随想:rand()还是arc4random()

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bj007.blog.51cto.com/1701577/544006 今天在iP ...

  5. 在 Golang 中使用 Protobuf

    wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gztar zxvf proto ...

  6. apply-register-acl 参数允许FreeSWITCH分机注册/拨打不验证密码

    今天调试 发现 注册的分机 的 `Auth-User` 居然是 `unknown` !!! 怎么回事? 仔细对比检查 发现, internal profile 指定了 `apply-register- ...

  7. Python 爬虫之 Scrapy 分布式原理以及部署

    Scrapy分布式原理 关于Scrapy工作流程 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享 ...

  8. etherboot无盘启动

    2001.10.30 吴峰光 本站提供对无盘启动的支持.本文就此作一简单介绍. 一.概述 无盘启动,更确切的说是网络启动,可算是最为轻松和简便的启动方式了. 目前还很少有人了解它,因为目前的软硬件条件 ...

  9. IIS “另一个程序正在使用此文件,进程无法访问"

    重启iis,提示"另一个程序正在使用此文件,进程无法访问".一开始就怀疑是80端口被占用,修改iis的端口为其它端口,重启iis, 果然成功. 在命令行,netstat -nabo ...

  10. JVM垃圾回收算法及回收器详解

    引言 本文主要讲述JVM中几种常见的垃圾回收算法和相关的垃圾回收器,以及常见的和GC相关的性能调优参数. GC Roots 我们先来了解一下在Java中是如何判断一个对象的生死的,有些语言比如Pyth ...