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 ...
随机推荐
- 判断是否AVL平衡二叉书
#include<iostream> #include<vector> #include<stack> #include<string> #includ ...
- 实践作业1:测试管理工具实践 Day1
1.熟悉课程平台2.选取小组作业工具并分工3.申请博客4.提交<高级软件测试技术SPOC2017年秋学生博客地址汇总>问卷5.着手熟悉Testlink
- 获取串口映射的COM端口号
背景:近期由于项目需要,需要操作短信猫,当短信猫插入电脑后,会根据当前PC状况,映射COM口,这里需动态获取短信猫映射的COM端口号. 编程语言C#: 具体代码如下 public enum Hardw ...
- ZBX_NOTSUPPORTED: Item does not allow parameters.
搞mongo监控的时候,zabbix报错:ZBX_NOTSUPPORTED: Item does not allow parameters. 想了半天,不知道原因,最后经过大神指点,原来是zabb ...
- gulp入门详细教程
简介:gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以很愉快的编写代码, ...
- Maven快速指南
Maven是Apache基金会提供的项目管理工具, 其采用项目对象模型(Project Object Model, POM)描述项目配置, 并使用生命周期模型管理构建过程中各种操作. POM mave ...
- 图片布局css
对于平时项目开发中,经常要展示图片.什么水平居中显示,垂直居中显示,水平或垂直居中显示...我们的发际线就是这样往后退的. 接下来要讲的就是对于各种图片布局的css实现(这里针对的是img标签的不会使 ...
- 运行期以索引获取tuple元素-C++11之2
//运行期以索引获取tuple元素-C++11之2 //需支持C++11及以上标准的编译器,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 //参见<深入应用C++ ...
- 利用linux shell自己主动顶贴
在论坛上面发帖问个什么东西的话,一旦不顶.帖子就秒沉了,可是又实在不想每时每刻都去顶,怎么办?以下展示了怎样利用shell 的crontab实现自己主动顶贴. 闲话不多说了,以豆瓣为例-– 1: 用c ...
- SQL或HQL预编译语句,可以防止SQL注入,可是不能处理%和_特殊字符
近期项目在做整改,将全部DAO层的直接拼接SQL字符串的代码,转换成使用预编译语句的方式.个人通过写dao层的单元測试,有下面几点收获. dao层代码例如以下 //使用了预编译sql public L ...