Maximum Subarray - LeetCode
题目链接
注意点
- 最大值有可能是正负数交替着出现
解法
解法一:一次遍历即可。当sum小于0的时候就重新开始求和,因为sum小于0再加上一个数字绝对不可能是max(即sum+nums[i] < nums[i])时间复杂度为O(n)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max = nums[0],sum = nums[0],i,n = nums.size();
for(i = 1;i < n;i++)
{
if(sum < 0) // when sum < 0, sum + nums[i] < nums[i]
{
sum = nums[i];
}
else
{
sum += nums[i];
}
if(sum > max)
{
max = sum;
}
}
return max;
}
};

小结
- 这道题有更多的解法,但是O(n)应该是最快的了。想起来这个还是我大一的时候从ACM培训讲座上学到的
Maximum Subarray - LeetCode的更多相关文章
- Maximum Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Maximum Subarray leetcode java
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 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 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
随机推荐
- 用Python做一个翻译软件
前两天吃了平哥的一波狗粮,他给女朋友写了一个翻译软件,自己真真切切的感受到了程序员的浪漫.在学习requests请求的时候做过类似的Demo,给百度翻译发送一个post请求可以实现任意词组的翻译,利用 ...
- Ubuntu解压zip包中文乱码
解决方法:通过unar 工具解压 步骤一: 安装unar: sudo apt-get install unrar 步骤二: 解压(以test.zip为例):unar test.zip 解压成功,乱码问 ...
- HTML学习1-Dom之事件绑定
事件: 1.注册事件 a. <div onxxxx=””></div> b. document .onxxxx= function() //找到这个标签 2.this,触发 ...
- 第七章 用户输入和while循环
7.1函数input()的工作原理 函数默认输入为字符串string,如果需使用数字,需用int进行类型转换 7.2 while循环 while是根据条件的真假判断是否进入执行 使用标志: 使用bre ...
- C++指定位数小数输出
关键词:头文件<iomanip>,指令setw(x),fixed,setprecision(x). setw()这个指令也可以配合setfill('')用于对齐输出,详情见另一篇博客htt ...
- md5sum命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/cbbbc/article/details/48563023 前言 在网络传输.设备之间转存.复制大文件等时,可能会出现传输 ...
- [leetcode-897-Increasing Order Search Tree]
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- C++ 函数 参数传递方式
用函数调用所给出的实参(实际参数,actual arguments)向函数定义给出的形参(形式参数,formal arguments)设置初始值的过程,叫做参数传递(Argument Passing) ...
- 互评Beta版本——二次元梦之队——“I Do”
基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 这是一款可以教学新手入门编程的软件,不断的通关让他们慢慢学会编程,可以让没有接触过编程的人了 ...
- 欢迎来怼--第二十九次Scrum会议
一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/17 15:55~16:25,总计30min. 地 ...