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 ...
随机推荐
- C# tostring
GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是一个通过特定算法产生 ...
- AndroidStudio cannot resolve symbol 解决办法 清楚缓存
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...
- Cannot load browser "PhantomJS": it is not registered! Perhaps you are missing some plugin? 测试安装遇到的BUG
安装了半天phantomjs就是安装不好,后面想了个死办法,http://phantomjs.org/download.html这个网址下先去下载好 phantomjs-2.1.1-windows.z ...
- 友元函数 C++
#include<iostream> #include<vector> using namespace std; class Text{ public: Text():a(){ ...
- 【Win 10 应用开发】将墨迹保存到图像的两种方法
IT界最近这几年,各种乱七八糟的东西不断出现,其中能用在实际工作与生活中的,大概也就那么几个.Web 前端也冒出各种框架,这就为那些喜欢乱用框架的公司提供了很好的机会,于是造成很多项目体积越来越庞大, ...
- php加密解密处理类
[PHP]代码 <?php /*=========================================================== = 版权协议: = GPL (The GN ...
- 雅虎WEB前端网站优化 -- 34条军规
雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条) 详细说明,下载转发 ponytail 的译文(来自帕兰映像). 1.Minimize HTTP Requests 减少HTTP请求 ...
- NOIP2017普及组初赛解析
首发于订阅号 嗨编程,这是一个以嗨为目标的编程订阅号(仅仅是目标而已),扫码可关注,不定期更.
- JAVA实现同域单点登录
所用技术: SSM MySQL Maven Tomcat8.0 同域单点登录详细步骤如下: 1.首先写一个登录界面(隐藏域为暂存地址) 2.判断用户密码是否正确,正确则添加cookie,否则返回错误页 ...
- Windows下Tomcat调优
windows tomcat 优化 1. tomcat conf server.xml 在server.xml中修改以一部分,增加节点数目,可以很好的提高性能: <Connector port ...