[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:/ ...
随机推荐
- 出售Illustrator脚本插件面板(包含面板源码,以及面板上所有的功能源码)
出售Illustrator脚本插件面板(包含面板源码,以及面板上所有的功能源码) 购买后可提供相应的小修改,以及教你使用往这个多列面里再加上按钮功能! 这套源码可作为工作使用,也可用为新手学习AI脚面 ...
- java动态调用webservice
cxf方式 public static Object[] invokeRemoteMethod(String url, String method, Object[] parameters) { Ja ...
- 二维码跳转不同的 app store
说道二维码 之前是用来跳转app store 然后在就是出来的 扫码付款什么的 用的很平常,其实里面也很简单 自己刚开始接触的时候 同事说要做一个二维码下载 应用 => 我=懵逼 ...
- cookie的存储和获取
在做用户登录时经常会用到cookie,如何将用户名和密码保存至cookie中呢?如何获取cookie中的数据呢? 一.用jquery.cookie.js保存数据 在页面内引入jQuery.cookie ...
- java回调机制
http://www.cnblogs.com/heshuchao/p/5376298.html
- 关于StringBuffer和StringBuilder
StringBuffer 字符串特点:字符串是常量:它们的值在创建之后不能更改. 字符串的内容一旦发生了变化,那么马上会创建一个新 的对象. 注意: 字符串的内容不适宜频繁修改,因为一旦修改马上就会创 ...
- Oracle 之SQL_Loader
SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PAR ...
- IOS __ 面试题
1.下面四种内部排序算法中哪一种在最差的情况下时间复杂度最高:(B) A.快速排序 B.冒泡排序 C.堆排序 D.归并排序 2.Shell中,将command1的输出作为command2的输入应该 ...
- Myeclipse下的struts2.3.8 配置 保证绝对好用
转自:http://blog.csdn.net/oxuannishi/article/details/8538386 1.建立web project工程,我的结构如下: 2.这一步非常重要:引入必要的 ...
- 远程登录VirtualBox虚拟机Linux
通过端口转发的方式,使用终端(如MobaXterm,Xshell,putty等终端)远程登录本机虚拟机Linux, 打开虚拟机,找到 [设置]-->[网络]--> [网卡1] 确认以下设置 ...