• 问题描述:

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.

中文翻译:

在至少有一个数字的数组内部找到一个连续的子数组,使其和为最大。

  • js代码
 /**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) { var maxEnd = nums[0];
var maxSofar = nums[0];
if(nums.length===0){
return 0;
}
if(nums.length==1){
return nums[0];
}
for(var i=1;i<nums.length;++i){ if(maxSofar<0){
maxSofar = nums[i];
}else{
maxSofar += nums[i];
}
maxEnd = Math.max(maxEnd,maxSofar);
}
return maxEnd; };

LeetCode中的最大子串和问题(Maximum Subarray)的更多相关文章

  1. leetcode解题报告(12):Maximum Subarray

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

  2. 算法:寻找maximum subarray

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

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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

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

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

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

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

  7. 【LeetCode】53. Maximum Subarray (2 solutions)

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

  8. Leetcode中字符串总结

    本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...

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

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

随机推荐

  1. 《java.util.concurrent 包源码阅读》 结束语

    <java.util.concurrent 包源码阅读>系列文章已经全部写完了.开始的几篇文章是根据自己的读书笔记整理出来的(当时只阅读了部分的源代码),后面的大部分都是一边读源代码,一边 ...

  2. EXTtreeGrid分页

    这个东西整了两天,关键在于要两个stroe,之所以要两个因为treestroe在分页的时候接受不了返回的总行数,不过有个问题就是第二页分页后显示在没有根节点的行层级显示不是那木明显: -------- ...

  3. 逆向知识第十四讲,(C语言完结)结构体在汇编中的表现形式

    逆向知识第十四讲,(C语言完结)结构体在汇编中的表现形式 一丶了解什么是结构体,以及计算结构体成员的对其值以及总大小(类也是这样算) 结构体的特性 1.结构体(struct)是由一系列具有相同类型或不 ...

  4. Http简单思维导图

  5. apache mysql无法启动解决办法

    最近在调试几个代码,需要不停的启动关闭服务器和mysql.在连续的几次开关后,无法启动了,每次启动后就直接关闭. 刚开始是怀疑是不是端口被占用了,查看端口后,并没有端口被占用的情况.查看mysql错误 ...

  6. WebApi接收复杂类型参数

    当接收实体时,该实体类不能添加Serializable属性,否则传来的json数据无法映射成功?

  7. sql server 2008 r2 登陆时显示无法打开默认的数据库

    解决! 第一步: 远程其他服务器的数据库能连上,本地的数据库某个用户名就是打不开,一开始以为是用户名或者密码错误, 后来用sqlcmd dos命令 -S . -U an -P sa 的方式登陆时可以的 ...

  8. 2016普及组t3海港

    好的,说说这道题的思路,爆搜队列嘛: 用一个结构体队列存每个人来的时间和他的国籍,用一个vis数组存每个人来的次数,是第一次来sum便加一. 然后从前面第一个人开始扔(原谅我用这个词,因为我找不到更好 ...

  9. [安全]PHP能引起安全的函数

    php中需要禁用以下函数来提高安全性 打开php.ini  找到 disable_functions .然后禁用以下函数 [C] 纯文本查看 复制代码 ? 1 disable_functions = ...

  10. 分布式监控系统Zabbix3.2对数据库的连接数预警

    在前篇分布式监控系统Zabbix3.2监控数据库的连接数 中已经对数据库的端口3306进行了监控,可以看到数据库的连接数历史变化有高有低,那如果达到了数据库连接数的阀值是不是主动通知给运维人员去检查问 ...