leetcode 虐我篇之(十三)Maximum Subarray
2014-08-10 00:44
232 查看
这道题也跟前面的一篇文章中所写的一样,都是在上周七夕的晚上做的。当时宿舍网络超不好,电脑又掉电。所以只能用手机敲代码,手机提交上去,当提交成功时,那种喜悦之情,简直难以言表。
Maximum Subarray这道题描述如下:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. 这道题目是要求最大连续和,我的思路是用两个变量记录当前的连续和还有最大连续和,开始时 sum = A[0],count = A[0]; 从第二个数开始遍历数组,将前面的计数结果加上当前位置的数,如果当前数比当前数加上前面的计数和还大,说明前面技术和出现了负数或者0,舍弃前面的计数和,将当前数设为计数和。接着判断计数和是否大于sum,是则更新之。当然了,因为这里是从第二个数开始的,要处理数组个数等于一个数或者小于等于0的情况。
下面是我的代码:
class Solution {
public:
int maxSubArray(int A[], int n) {
if(0 >= n) return 0;
if(1 == n) return A[0];
int sum = A[0],count = A[0];
for(int i = 1; i < n; i++)
{
//将前面的计数结果加上当前位置的数
count += A[i];
//如果当前数比当前数加上前面的计数和还大,说明前面技术和出现了负数或者0,
//舍弃前面的计数和,将当前数设为计数和
if(A[i] >= count)
{
count = A[i];
}
//如果计数和大于sum,则更新之
if(count > sum)
{
sum = count;
}
}
return sum;
}
};
做完这道题后,看了别人的一些解题对照了一下,发现我的解法竟然是动态规划解。之前本科acm校内选拔赛的时候看过一下DP问题,现在也很多忘了,幸好这几天又翻看了一下《算法导论》。还有另外的一种解法是分治法,就是题目后面的延伸中讲到的,看了一下,就是用递归。有兴趣的同学可以看一下。
Maximum Subarray这道题描述如下:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. 这道题目是要求最大连续和,我的思路是用两个变量记录当前的连续和还有最大连续和,开始时 sum = A[0],count = A[0]; 从第二个数开始遍历数组,将前面的计数结果加上当前位置的数,如果当前数比当前数加上前面的计数和还大,说明前面技术和出现了负数或者0,舍弃前面的计数和,将当前数设为计数和。接着判断计数和是否大于sum,是则更新之。当然了,因为这里是从第二个数开始的,要处理数组个数等于一个数或者小于等于0的情况。
下面是我的代码:
class Solution {
public:
int maxSubArray(int A[], int n) {
if(0 >= n) return 0;
if(1 == n) return A[0];
int sum = A[0],count = A[0];
for(int i = 1; i < n; i++)
{
//将前面的计数结果加上当前位置的数
count += A[i];
//如果当前数比当前数加上前面的计数和还大,说明前面技术和出现了负数或者0,
//舍弃前面的计数和,将当前数设为计数和
if(A[i] >= count)
{
count = A[i];
}
//如果计数和大于sum,则更新之
if(count > sum)
{
sum = count;
}
}
return sum;
}
};
做完这道题后,看了别人的一些解题对照了一下,发现我的解法竟然是动态规划解。之前本科acm校内选拔赛的时候看过一下DP问题,现在也很多忘了,幸好这几天又翻看了一下《算法导论》。还有另外的一种解法是分治法,就是题目后面的延伸中讲到的,看了一下,就是用递归。有兴趣的同学可以看一下。
相关文章推荐
- 每周LeetCode算法题(十三)152. Maximum Product Subarray
- leetcode(十三)Add Two Numbers
- LeetCode刷题系列(十三)SingleNumber && MajorityNumber
- Leetcode 题解系列(十三)
- LeetCode学习篇十三——Perfect Squares
- leetcode题目例题解析(十三)
- 深度复制链表——Leetcode系列(十三)
- LeetCode的medium题集合(C++实现)十三
- 给使用“十三 WebShell”后门黑客的重磅炸弹
- [leetcode]String to Integer (atoi)
- java提高篇(十三)-----字符串
- Leetcode: Same Tree
- leetcode 之 Flatten Binary Tree to Linked List 解决思路
- Leetcode: Median of Two Sorted Arrays
- 【LeetCode】Anagrams 解题报告
- leetcode——Valid Sudoku
- LeetCode Minimum Depth of Binary Tree
- 【leetcode c++】28 Implement strStr()
- leetcode 题解代码整理 11-15题
- LeetCode题解——Reverse Linked List II