LeetCode中的最大子串和问题(Maximum Subarray)
- 问题描述:
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)的更多相关文章
- leetcode解题报告(12):Maximum Subarray
描述 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- Leetcode中字符串总结
本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
随机推荐
- (二)部署solr7.1.0到tomcat
solr7.1.0部署到tomcat8 官方表示solr5之后的版本不再提供对第三方容器的支持(不提供war包了). "旧式"solr.xml格式不再支持,核心必须使用core.p ...
- linux下expect命令实现批量ssh免密
有时候我们需要批量发送ssh命令给服务器,但是有可能有些服务器是新加入的,还没有配置ssh免密,这个时候就会提示我们输入yes/no 或者password等,expect脚本命令就是用于在提示这些的时 ...
- TFBOY 养成记 一些比较好多文章。
API解释中文版(简书文章,没事看看): http://www.jianshu.com/p/e3a79eac554f Tensorlfow op辨异:tf.add()与tf.nn.bias_add() ...
- 邻里街坊 golang入坑系列
如果要追新或者怀旧,就点击https://andy-zhangtao.gitbooks.io/golang/content/ . 博客园里面的文章基本和gitbook上面是保持同步的. 这几天看了几集 ...
- RFID电动自行车与共享单车之物联网比较
目前比较热门的RFID电动自行车管理和共享单车,都是属于物联网范畴.它们之间有什么不同呢? 1.RFID电动自行车管理系统原理 RFID电动自行车管理,利用了有源RFID技术,使用基站SR8读取安装在 ...
- 自己动手编写IOC框架(二)
万事开头难,上篇已经起了一个头,之后的事情相对就简单了.上次定义了框架所需的dtd也就是规定了xml中该怎么写,有哪些元素.并且我们也让dtd和xml绑定在了一起,使dtd对xml的格式进行校验,并且 ...
- Springmvc ModelAndView踩过的坑之HttpServletResponse response
先抛出问题.以下两个方法声明有毛区别: @RequestMapping(value = "/rg") public void rg(@PathVariable Long pageI ...
- UWP Listview 多选
最近在做一个项目的时候,用到了Listview,需要选择一个item,来进行删除. 但是当开启了 PullToRefreshListViewControl.IsMultiSelectCheckBoxE ...
- 用 Label 控制 Service 的位置 - 每天5分钟玩转 Docker 容器技术(106)
上一节我们讨论了 Service 部署的两种模式:global mode 和 replicated mode.无论采用 global mode 还是 replicated mode,副本运行在哪些节点 ...
- 树莓派搭建pptp---vpn
好久没写博文了啊,这次好好写 先普及下知识啊 PTP(Point to Point Tunneling Protocol),即点对点隧道协议.该协议是在PPP协议的基础上开发的一种新的增强型安全协议, ...