前言

 

【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.

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.

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的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  5. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  6. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  7. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  8. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  9. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

随机推荐

  1. django之管理静态文件

    管理静态文件 项目中的CSS.图片.js都是静态文件 配置静态文件 在settings 文件中定义静态内容 STATIC_URL = '/static/' STATICFILES_DIRS = [ o ...

  2. mongodb(二)

    数据准备: var persons = [{ name:"jim", age:25, email:"75431457@qq.com", c:89,m:96,e: ...

  3. firefox 插件 取消认证签名

    Firebug Tab Mix plus :系统退出自动保存tab List.     tab mix options>Session>start/exit>when browse ...

  4. 我的Linux之路——xshell连接linux虚拟机

    出自:https://www.linuxidc.com/Linux/2016-08/134087.htm xshell 5登录本地虚拟机的具体操作步骤如下: 1.首先打开虚拟机,登录到操作系统; 2. ...

  5. idea使用jrebel热部署插件

    首先通过idea下载JReble插件 访问http://idea.lanyus.com/网站 跟着弹出框的步骤走就可以实现了: 在idea中使用tomcat部署项目 不要使用war:使用下面的文件

  6. 怎样获取datagrid中编辑列combobox的value值与text值

    var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'}); var productname = $(ed ...

  7. 【原】Coursera—Andrew Ng机器学习—Week 9 习题—异常检测

    [1]异常检测 [2]高斯分布 [3]高斯分布 [4] 异常检测 [5]特征选择 [6] [7]多变量高斯分布 Answer: ACD B 错误.需要矩阵Σ可逆,则要求m>n  测验1 Answ ...

  8. 获取web.xml配置文件中的初始化值

    TestServletConfig.java package com.huawei.config; import java.io.IOException;import java.util.Enumer ...

  9. Django框架开发web网站的网页优化—页面静态化

    网站优化-页面静态化 1)概念 提前将页面所用到的数据从数据库查询出来,然后生成一个静态页面,之后用户来访问的时候,直接返回静态页面. 举例:首页静态化:获取首页用到的数据表中的数据,生成静态首页in ...

  10. Finite State Transducers

    一, 简介 Finite State Transducers 简称 FST, 中文名:有穷状态转换器.在自然语言处理等领域有很大应用,其功能类似于字典的功能(STL 中的map,C# 中的Dictio ...