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.

 1 public class Solution {
 2     public int maxSubArray(int[] A) {
 3         if(A.length<=0) return 0;
 4         int max = Integer.MIN_VALUE;
 5         int sum = 0;
 6         for(int i=0;i<A.length;i++){
 7             sum +=A[i];
 8             if(sum>max){
 9                 max = sum;
10             }
11             if(sum<0){
12                 sum = 0;
13             }
14         }
15         return max;
16     }
17 }

View Code

What if multiply?

转载于:https://www.cnblogs.com/krunning/p/3538732.html

Maximum Subarray with Sum/ Multiply相关推荐

  1. Lintcode42 Maximum Subarray II solution 题解

    [题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...

  2. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  3. [LeetCode] Maximum Subarray 最大子数组

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

  4. [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray

    41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...

  5. Leetcode-53 Maximum Subarray

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

  6. 53.Maximum Subarray

    /** 53.Maximum Subarray * 2016-5-7 by Mingyang * 如果我们从头遍历这个数组.对于数组中的其中一个元素,它只有两个选择: 1.* 要么加入之前的数组加和之 ...

  7. LeetCode(53):Maximum Subarray

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

  8. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  9. LeetCode: Maximum Subarray 解题报告

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

最新文章

  1. linux oracle 脚本,Linux的Oracle服务脚本
  2. 正则判断 手机邮箱的正确格式
  3. UNIX 网络协议的深度分析
  4. 死锁解决(oracle)
  5. Andorid App内部跟随熊语言改变
  6. SQL中CONVERT转化函数的用法[个人推荐]
  7. 求矩阵不靠边元素之和_机器(深度)学习数学知识之范数
  8. 无人机技术在精细建模中的应用(PPT)
  9. Android studio显示红叉,提示Error:please select android sdk
  10. sim卡没坏但苹果手机无服务_苹果8PLUS换壳导致无服务维修
  11. ScreenToGif录制录屏gif软件的推荐通用设置,优化使用体验
  12. 洛谷P1359租用游艇
  13. Unity人工智能之不断自我进化的五人足球赛
  14. 苹果系统 macOS Mojave 10.14.4上安装 Ubuntu 18.04 双系统
  15. Quartz 2D 涂鸦
  16. 八大基本数据类型(primitive type)
  17. Oracle函数获取汉字的首字母
  18. 逆元 与等比数列求和
  19. Broadcast 的使用
  20. 09年生日送我的煎蛋超级技术

热门文章

  1. Spring Cloud:查看注册到Eureka上的应用信息
  2. web实现远程桌面:Apache Guacamole
  3. typecho引入php文件,Typecho的functions.php
  4. html5家谱资源网,免费家谱系统(ASP,Access,CSS,html5)
  5. 重庆计算机二本专业有哪些专业,重庆高考计算机类分数线
  6. ajax删除成功后的回调,Ajax成功回调'删除'不触发在Firefox中
  7. python多进程优化_如何利用多进程优化Python视频应用
  8. 一行代码不用写,就可以训练、测试、使用模型,这个 star 量 1.5k 的项目帮你做到...
  9. 提前(qian)庆祝 “1024程序员节” ,价值 1500 元的京东卡免费送!
  10. 大家都在看的公众号是哪几个?