【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 ...
随机推荐
- Paper Reviews and Presentations
Copied from Advanced Computer Networks, Johns Hopkins University. Paper Reviews and Presentations Ea ...
- HTML基本内容
设置背景色:<body bgcolor="#AAAAAA">,设置背景图:<body background="1.png">. 颜色的知 ...
- 关于导入geoserver 源码到Eclipse编译运行
参考http://blog.csdn.net/gisshixisheng/article/details/43016443 和 http://blog.sina.com.cn/s/blog_6e37 ...
- jsTree使用
引用:jsTreede css 与Js 初始化jsTree: //加载树 function initTree(treeData) { $.jstree.destroy(); $('#treeDiv') ...
- 生成器的使用demo
定义一个函数: def frange(start, stop, increment): x = start while x < stop: yield x x += increment 使用: ...
- Servlet模板,一个供新手参考的模板
由于这学期老师的进度是刚开始教JavaSE部分,而我的进度比较快,所以买了3本javaee的书,我根据自己的基础,选择了合适的开发实践,另外两本书都和框架相关,我自认为我的web基础还不是很牢固,所以 ...
- VC.时间(网页内容收集)
1.VC++获得当前系统时间的几种方案_记忆53秒_新浪博客.html(http://blog.sina.com.cn/s/blog_676271a60101i0hb.html) 1.1.内容保存: ...
- async await 多线程
async await 并没有开启多线程 出现await的地方 只是开启了一个子线程继续往后执行 主线程返回 防止阻塞 相当于 await customerRepository.getall() ...
- Android AndFix修复方式的限制
这里阅览了很多网上关于修复的资料,一一贴在这里便于查看: https://github.com/alibaba/AndFix 这是官方处 要了解使用,一定得看看这里. http://www.jia ...
- js判断字符串与字符串相互包含,以及数组是否包含某个元素;
需求:判端一个字符串是否包含另一个字符串? 实现: var str = "adc"; 判断str 中是否包含 "c" if( str.indexOf(&quo ...