[LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum.
Notice
The subarray should contain at least one number.
Given the array [−2,2,−3,4,−1,2,1,−5,3]
, the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
Can you do it in time complexity O(n)?
LeetCode上的原题,请参见我之前的博客Maximum Subarray。
解法一:
- class Solution {
- public:
- /**
- * @param nums: A list of integers
- * @return: A integer indicate the sum of max subarray
- */
- int maxSubArray(vector<int> nums) {
- int res = INT_MIN, curSum = ;
- for (int num : nums) {
- curSum += num;
- curSum = max(curSum, num);
- res = max(res, curSum);
- }
- return res;
- }
- };
解法二:
- class Solution {
- public:
- /**
- * @param nums: A list of integers
- * @return: A integer indicate the sum of max subarray
- */
- int maxSubArray(vector<int> nums) {
- if (nums.empty()) return ;
- return helper(nums, , (int)nums.size() - );
- }
- int helper(vector<int>& nums, int left, int right) {
- if (left >= right) return nums[left];
- int mid = left + (right - left) / ;
- int lmax = helper(nums, left, mid - );
- int rmax = helper(nums, mid + , right);
- int mmax = nums[mid], t = mmax;
- for (int i = mid - ; i >= left; --i) {
- t += nums[i];
- mmax = max(mmax, t);
- }
- t = mmax;
- for (int i = mid + ; i <= right; ++i) {
- t += nums[i];
- mmax = max(mmax, t);
- }
- return max(mmax, max(lmax, rmax));
- }
- };
[LintCode] Maximum Subarray 最大子数组的更多相关文章
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 【LeetCode每天一题】Maximum Subarray(最大子数组)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Maximum Subarray(最大子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LintCode: Maximum Subarray
1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...
- [Leetcode] maximun subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
随机推荐
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
- B+Tree和MySQL索引分析
首先区分两组概念: 稠密索引,稀疏索引: 聚簇索引,非聚簇索引: btree和mysql的分析: 参见 http://blog.csdn.net/hguisu/article/details/7786 ...
- ajax用get刷新页面元素在IE下无效解决~~
总结一下解决办法: 在IE下用Ajax请求某一页面,通常会因为缓存的原因而返回上一次的结果,造成混乱,[即get方式时,获取数据,因发送参数和地址都一致,故IE浏览器会从缓存中取,而不会去请求服务器端 ...
- MySQL存储过程动态SQL语句的生成
用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...
- js中的斐波那契数列法
//斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...
- JS脚本语言是什么意思?
javascript,Javascript是一种浏览器端的脚本语言,用来在网页客户端处理与用户的交互,以及实现页面特效.比如提交表单前先验证数据合法性,减少服务器错误和压力.根据客户操作,给出一些提升 ...
- SQL注入以及如何防止和索引
SQL注入产生的原因:程序开发过程中不注意规范书写sql语句和对特殊字符进行过滤,导致客户端可以通过全局变量POST和GET提交一些sql语句正常执行. 防止SQL注入: 1.开启配置文件中的magi ...
- 1250 Super Fast Fourier Transform(湘潭邀请赛 暴力 思维)
湘潭邀请赛的一题,名字叫"超级FFT"最终暴力就行,还是思维不够灵活,要吸取教训. 由于每组数据总量只有1e5这个级别,和不超过1e6,故先预处理再暴力即可. #include&l ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- 前端项目通用、常用js common.js
var url = location.href; if (url.toLowerCase().indexOf("/akweb_admin/") == -1) { function ...