要求时间复杂度 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. JUnit 4 Vs TestNG比较

    JUnit 4和TestNG都是Java中非常受欢迎的单元测试框架.两种框架在功能上看起来非常相似. 哪一个更好? 在Java项目中应该使用哪个单元测试框架? 下面表中概括了JUnit 4和TestN ...

  2. 插件写法之脚本运行环境,mac和window检测

    (function(root, factroy){   /* * 在这里进行对脚本运行环境的检测判断 * 浏览器中 有window对象 * node.js服务器端 有Global对象 * * IE11 ...

  3. 排序——冒泡排序(java描述)

    百度百科:冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果他们的顺序(如从大到小.首字母从A到Z)错误就把他们交 ...

  4. js下数据库 nedb lokijs

    一个7千颗星nedb.一个3千 lokijs  https://rawgit.com/techfort/LokiJS/master/jsdoc/tutorial-Persistence%20Adapt ...

  5. CC2 条理分明-----AACTP教你谈恋爱

      AACTP是一个神奇的地方,这里可以培养你的自信,培养你的沟通表达能力,培养你的领导管理能力:但是你不知道的是AACTP还可以培养你恋爱的本领.想知道是怎么回事吗?听我给你慢慢说来.我把恋爱分为三 ...

  6. AtCoder Regular Contest 102 D - All Your Paths are Different Lengths

    D - All Your Paths are Different Lengths 思路: 二进制构造 首先找到最大的t,使得2^t <= l 然后我们就能构造一种方法使得正好存在 0 到 2^t ...

  7. JAVA基础知识总结:二十

    一.网络编程基础 1.概念 所谓计算机网络,就是把分布在不同区域的计算机与专门的外部设备使用通信线路连接成一个规模大,功能比较强的网络系统,从而使得计算机之间可以相互通信,共享资源 所谓的网络编程,在 ...

  8. boostrapt的二级下拉菜单

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta conte ...

  9. redflag的echarts结构

    总体的市场情况 这里我需要4个data数组. var list = { currentData:[],//这里表示当月数据量,数组长度8 totalData:[],//这里表示的累计数据量,数组长度8 ...

  10. 电脑用HDMI线分屏后,耳机或音箱没声音之完美解决!

    现今,由于工作需要,很多人都偏爱给自己的电脑进行分屏,两个显示器同时连接在一台电脑上,并进行“扩展这些功能显示”,从而大大提高了工作积极性和工作效率.本人在进行分屏后遇到了耳机没有声音的问题,并进行了 ...