[LeetCode 题解]: Maximum Subarray
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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
.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
2. 解法
1 class Solution {
2 public:
3 int maxSubArray(int A[], int n) {
4 int sum,ans,i,mark=0;
5 sum=ans=i=0;
6
7 for(i=0;i<n;i++)
8 if(A[i]>=0) mark=1;
9
10 if(mark==0)
11 {
12 ans=A[0];
13 for(i=1;i<n;i++)
14 if(A[i]>ans) ans=A[i];
15
16 }
17 else
18 {
19 for(i=0;i<n;i++)
20 {
21 sum+=A[i];
22 if(sum<0)
23 sum=0;
24 if(sum>ans) ans=sum;
25 }
26 }
27 return ans;
28 }
29 };
3. 相关题目
Gas Station 题解 http://www.cnblogs.com/double-win/p/3746637.html
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3746672.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Maximum Subarray的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
随机推荐
- openStack nova nova valid hosts 优化
scheduler_default_filters=AllHostsFilterallow_resize_to_same_host=Trueallow_migrate_to_same_host=Tru ...
- maven-resources-plugin使用
命令行中带参数指定${}变量值 <build> <resources> <resource> <directory>src/main/resources ...
- sql中经度维度计算距离
------------------------------------------创建一个方法---------------------------------------------------- ...
- iOS学习之Xcode 的Debug技巧
在Xcode中,Debug时,不能像eclipse ,或VS那些集成开发那样,能直接查看变量的值.那怎么在调试的时候查看XCode的变量呢? 有一些方法的. 1.新建一个Single View App ...
- 【转】Pixel-Fillrate
[Pixel-Fillrate] “填充率“以每秒钟填充的像素点为单位,“三角形(多边形)生成速度“则表示每秒钟三角形(多边形)生成个数.现在的3D显卡的性能也主要看着两项指标,这两项指标的数值越大, ...
- Platform Dependent Compilation
[Platform Dependent Compilation] 1.Platform Defines 2.在Project Setting -> Player 面板的Other Setting ...
- 01 lucene基础 北风网项目培训 Lucene实践课程 Lucene概述
lucene-core-2.4.1.jar是lucene开发的核心jar包,lucene-analyzers-2.4.1.jar也是必不可少的.lucene-highlighter-2.4.1.jar ...
- 【poj1743】Musical Theme 【后缀自动机】
题意 给出一个n个数字的序列,找出相同变化趋势且不重叠的两个最长子串. 分析 这个题以前应该用后缀数组+二分做过.学了后缀自动机后可以用后缀自动机搞一下. 先差分,然后把查分后的数组建SAM.然后对于 ...
- 112. Path Sum (Tree; DFS)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- SuperWebSocket
SuperWebSocket: 概述:SuperWebSocket是WebSocket服务器的.NET实现. 简介:WebSocket是通过单个传输控制协议(TCP)插座提供双向,全双工通信信道的技术 ...