要求时间复杂度 O(n)。

e.g. 给定数组 [-2,1,-3,4,-1,2,1,-5,4],其中有连续子序列 [4,-1,2,1] 和最大为 6。

我完全没有想法,看了答案。

C++实现:

 int maxSubArray(vector<int>& nums) {
int result = nums[], sum = ;
for (int i = ; i < nums.size(); i++) {
sum = max(sum, );
sum += nums[i];
result = max(sum, result);
}
return result;
}

或者

 int maxSubArray(vector<int>& nums) {
int result = nums[], sum = ;
for (int i = ; i < nums.size(); i++) {
sum = max(sum + nums[i], nums[i]);
result = max(sum, result);
}
return result;
}

【LeetCode】最大子序列和的更多相关文章

  1. leetcode 字符串动态规划总结

    问题1:leetcode 正则表达式匹配 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配 ...

  2. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  3. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  4. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  5. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  6. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  8. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  9. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  10. [LeetCode] Longest Harmonious Subsequence 最长和谐子序列

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

随机推荐

  1. 火狐对 min-height 的支持

    代码: <!DOCTYPE html> <style> .com-center-banner { background: #f00; } .com-center-banner ...

  2. 1st,Python基础——01

    1 Python介绍 2 Python发展史 3 Python2 or 3? 4 Python安装 就不写了,各路大牛的博客都很详细. 5 Hello World程序 #!/usr/bin/env p ...

  3. java中垃圾回收算法讲解

      判断对象是否存活的方法: 一.引用计数算法(Reference Counting) 介绍:给对象添加一个引用计数器,每当一个地方引用它时,数据器加1:当引用失效时,计数器减1:计数器为0的即可被回 ...

  4. template-web.js 引用变量、函数

    1.关键字   $imports.+变量/函数 {{if $imports.myParseInt(b.health_money)}} <span class="num"> ...

  5. /usr/bin/perl:bad interpreter:No such file or directory 的解决办法

    yum -y install gcc gcc-c++ perl make kernel-headers kernel-devel 可能会提示:Cannot find a valid baseurl f ...

  6. Mongodb 创建管理员帐号与普通帐号

    数据库操作权限 readAnyDatabase 任何数据库的只读权限 userAdminAnyDatabase 任何数据库的读写权限 userAdminAnyDatabase 任何数据库用户的管理权限 ...

  7. python模块(4)

    re正则 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回(没有group()方法) re.splitall 以匹配到的字符 ...

  8. U8工具栏特别小是怎么回事

    用友的工具栏特别窄了,填制凭证里的保存.增加凭证等按钮因为工具栏特别窄都看不清了 解决方法:正常机器下的system32下面的mscomctl.ocx文件替换到有问题的机器下,您的系统应该是XP的,这 ...

  9. java web 方面

    1.Tomcat的优化经验. 2.http请求的GET与POST方式的区别. (1)get是从服务器上获取数据,post是向服务器传送数据. (2)get是把参数数据队列加到提交表单的ACTION属性 ...

  10. ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang

    Ka Chang 思路: dfs序+树状数组+分块 先dfs处理好每个节点的时间戳 对于每一层,如果这一层的节点数小于sqrt(n),那么直接按照时间戳在树状数组上更新 如果这一层节点个数大于sqrt ...