【LeetCode】最大子序列和
要求时间复杂度 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】最大子序列和的更多相关文章
- leetcode 字符串动态规划总结
问题1:leetcode 正则表达式匹配 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LeetCode] Longest Harmonious Subsequence 最长和谐子序列
We define a harmonious array is an array where the difference between its maximum value and its mini ...
随机推荐
- 【搬运工】——Java中的static关键字解析(转)
原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大 ...
- 【Python】【有趣的模块】【requests】【一】HTTP头信息总结
[HTTP请求 == 请求行 + 消息报头 + 请求正文 ] 请求行:Method Request-URL HTTP-Version CRLF HTTP协议定义了许多与服务器交互的方法 ① PUT:请 ...
- _rate_charaters
该表可以控制特定玩家的掉率 guid 玩家角色guid,characters表中guid rate 掉落倍率,比如1.1,则该玩家普通掉率(groupid = 0时)提高1.1倍
- Python 循环与定义函数
break for i in range(10): if i == 2: break print i 0 1 continue for i in range(10): if i == 2: conti ...
- hbase非结构化数据库与结构化数据库比较
目的:了解hbase与支持海量数据查询的特性以及实现方式 传统关系型数据库特点及局限 传统数据库事务性特别强,要求数据完整性及安全性,造成系统可用性以及伸缩性大打折扣.对于高并发的访问量,数据库性能不 ...
- 如何编写一个d.ts文件
这篇文章主要讲怎么写一个typescript的描述文件(以d.ts结尾的文件名,比如xxx.d.ts). 2018.12.18更新说明: 1.增加了全局声明的原理说明. 2.增加了es6的import ...
- Java 8里面lambda的最佳实践
Java 8已经推出一段时间了,越来越多开发人员选择升级JDK,这条热门动弹里面看出,JDK7最多,其次是6和8,这是好事! 在8 里面Lambda是最火的主题,不仅仅是因为语法的改变,更重要的是带来 ...
- 全排列问题(c语言实现)
问题描述: 假设有数组里面存放26个字母,取出n个,以m个排列,计算排列的总数! 注意: (1) m<n (2) 里面的元素不能重复排列 (3)"遇零则止" 核心代码如下: ...
- 06-python-生成器、循环器
生成器 生成器(generator)的主要目的是构成一个用户自定义的循环对象. 生成器的编写方法和函数定义类似,只是在return的地方改为yield.生成器中可以有多个yield.当生成器遇到一个y ...
- nodejs导出excel
//导出Excel var nodeExcel = require("excel-export"); var fs = require("fs"); var c ...