leetcode97:maximum -subarray
题目描述
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.
If you have figured out the O(n) solution, try coding another
solution using the divide and conquer approach, which is more subtle.
输出
6
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @return int整型
*/
int maxSubArray(int* A, int n) {
// write code here
int sum=A[0],maxSum=A[0];
for (int i=1;i<n;i++){
if (sum<0)
sum=0;
sum+=A[i];
maxSum=max(maxSum,sum);
}
return maxSum;
}
};
leetcode97:maximum -subarray的更多相关文章
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
随机推荐
- Mat对象与像素操作 OpenCV C++
Mat对象,分为两个部分,头部和数据部分 Mat对象拷贝之后是相互独立的 Mat对象有三种创建方法 CV_8UC1单通道,CV_8UC2双通道,CV_8UC3三通道,通道数 Scalar(0-255, ...
- vue去掉地址栏#号
mode:'history' 将这代码放入router.js里面
- 如何让程序像人一样的去批量下载歌曲?Python爬取付费歌曲
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 今天来教大家一个自动化爬虫的工具 selenium selenium Se ...
- 使用AirtestProject+pytest做支付宝小程序UI自动化测试
一,前言 1,背景 因公司业务需要做支付宝小程序的UI自动化测试,于是在网上查找小程序的自动化资料,发现微信小程序是有自己的测试框架的,但几乎找不到支付宝小程序UI自动化测试相关的资料.白piao失败 ...
- CSS的背景
CSS的背景 1. 背景颜色background-color div { background-color: 颜色值; } 一般情况下元素背景颜色默认是transparent(透明). 2. 背景图片 ...
- 浅谈Python常用英文单词
一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7.charac ...
- 【Linux教程】Linux系统零基础编程入门,想当大神?这些你都要学
✍ 文件和文件系统 文件是Linux系统中最重要的抽象,大多数情况下你可以把linux系统中的任何东西都理解为文件,很多的交互操作其实都是通过文件的读写来实现的. 文件描述符 在Linux内核中,文件 ...
- rabbitmq 延时队列 插件方式实现 每条消息都延时自己时间
上篇文章的延时是加到队列上的 通过死信过时推送 ,缺点就是不能每条消息定义自己的过时时间而且每次有新的过时时间,要新建一个交换机和队列 https://www.cnblogs.com/brady-wa ...
- Hadoop框架:DataNode工作机制详解
本文源码:GitHub·点这里 || GitEE·点这里 一.工作机制 1.基础描述 DataNode上数据块以文件形式存储在磁盘上,包括两个文件,一个是数据本身,一个是数据块元数据包括长度.校验.时 ...
- JAVA 线上故障排查套路,从 CPU、磁盘、内存、网络到GC 一条龙!
线上故障主要会包括cpu.磁盘.内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍. 同时例如jstack.jmap等工具也是不囿于一个方面的问题的, ...