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. 4_python之路之模拟工资管理系统

    python之路之模拟工资管理系统 1.程序说明:Readme.txt 1.程序文件:salary_management.py info.txt 2.程序文件说明:salary_management. ...

  2. 浅析ECMP等价路由

    1.ECMP简介 Equal-CostMultipathRouting,等价多路径.即存在多条到达同一个目的地址的相同开销的路径.当设备支持等价路由时,发往该目的 IP 或者目的网段的三层转发流量就可 ...

  3. Rhythmk 一步一步学 JAVA(9) JAVA 基础笔记[枚举,...]

    1.装箱就是值类型转换为object类型,拆箱相反:object转化为值类型 eg:Integer i=1; // 装箱 int j=i; // 拆箱 2.静态导入: eg: 导入: import s ...

  4. jQuery自动触发事件

    转自:https://blog.csdn.net/CY_LH/article/details/78982218 常用模拟 有时候,需要通过模拟用户操作,来达到单击的效果.例如在用户进入页面后,就触发c ...

  5. 前端使用CryptoJs类库进行sha-256、MD5加密

    Google的加密库 CryptoJs(点此下载) 包含了很多常用的加解密方式,包括AES.DES.SHA-1.SHA-2.SHA256.MD5等. DES对称加密在之前的文章中也有介绍过,大传送门. ...

  6. Bootstrap 与 Jquery validate 结合使用——简单实现

    首先必须引入的JS和CSS <script type="text/javascript" src="${ctx}/static/js/jquery-1.9.1.mi ...

  7. 使用Tor创建.onion域名网站(创建暗网服务和暗网的网站)

    使用Tor 的.onion域名创建匿名服务器 Tor不仅可以提供客户端的匿名访问,Tor还可以提供服务器的匿名.通过使用Tor网络,用户可以维护位置不可知的服务器.当然如果要访问这个隐蔽的服务,客户端 ...

  8. Zabbix Agent 自动、主动注册

    简述: 今天来研究一下 Zabbix 的主动注册功能. 当你有十台机器需要监控时,你手动去添加是没有问题的.但是当你有五十台.上百台或更多服务器要监控时,你会怎么做 ? Active Agent Au ...

  9. LeetCode Crack Note --- 1. Two Sum

    Discription Given an array of integers, return indices of the two numbers such that they add up to a ...

  10. lucene3.0范围查找

    在lucene3.0以上版本中,范围查询也有很大的变化,RangeQuery已经不推荐使用,使用TermRangeQuery和NumericRangeQuery两个替代.TermRangeQuery: ...