/**** Source : https://oj.leetcode.com/problems/maximum-subarray/** Created by lverpeng on 2017/7/18.** 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.**/
public class MaxSubarray {/*** 找到所有子数组中最大的和** 数组从前向后遍历,针对每个数组元素有两个选择,要么加入已经存在子数组,如果该元素的值大于该元素和前面数组总和的和还要大,*          那就重新开始一个新的子数组,遍历完数组找到最大的和*** @param arr* @return*/public int maxSubarray (int[] arr) {int loopCount = 0;int[] sum = new int[arr.length];int max = 0;sum[0] = arr[0];for (int i = 1; i < arr.length; i++) {sum[i] = Math.max(arr[i], sum[i - 1] + arr[i]);max = Math.max(max, sum[i]);loopCount ++;}System.out.println("maxSubarray-->" + loopCount);return max;}public int maxSubarray1 (int[] arr) {int loopCount = 0;// 只记录上一个和int sum = 0;int max = 0;for (int i = 1; i < arr.length; i++) {if (sum < 0) {sum = 0;}sum += arr[i];max = Math.max(max, sum);loopCount ++;}System.out.println("maxSubarray1-->" + loopCount);return max;}/*** 使用分治法** @param arr* @return*/int loopCount1 = 0;public int maxSubarray2 (int[] arr) {loopCount1 = 0;return divide(arr, 0, arr.length - 1);}private int divide (int[] arr, int low, int high) {if (low == high) {return arr[low];}if (low == high - 1) {return Math.max(arr[low] + arr[high], Math.max(arr[low], arr[high]));}int mid = (low + high) / 2;int lmax = divide(arr, low, mid - 1);int rmax = divide(arr, mid + 1, high);int mmax = arr[mid];int temp = mmax;for (int i = mid - 1; i > 0; i--) {temp += arr[i];if (mmax < temp) {mmax = temp;}loopCount1 ++;}temp = mmax;for (int i = mid + 1; i < high; i++) {temp += arr[i];if (temp > mmax) {mmax = temp;}loopCount1 ++;}System.out.println("maxSubarray2-->" + loopCount1);return Math.max(mmax, Math.max(lmax, rmax));}public static void main(String[] args) {MaxSubarray maxSubarray = new MaxSubarray();int[] arr = new int[]{-2,1,-3,4,-1,2,1,-5,4};System.out.println(maxSubarray.maxSubarray(arr));System.out.println(maxSubarray.maxSubarray1(arr));System.out.println(maxSubarray.maxSubarray2(arr));int[] arr1 = new int[]{-2,1,-3,4,-1,2,1,-5,4,-2,1,-3,4,-1,2,1,-5,4,-2,1,-3,4,-1,2,1,-5,4,-2,1,-3,4,-1,2,1,-5,4};System.out.println(maxSubarray.maxSubarray(arr1));System.out.println(maxSubarray.maxSubarray1(arr1));System.out.println(maxSubarray.maxSubarray2(arr1));}
}

转载于:https://www.cnblogs.com/sunshine-2015/p/7524364.html

leetcode — maximum-subarray相关推荐

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

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

  2. LeetCode: Maximum Subarray 解题报告

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

  3. [LeetCode] Maximum Subarray

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

  4. LeetCode Maximum Subarray

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

  5. LeetCode - Maximum Subarray

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

  6. LeetCode: Maximum Subarray

    自己想得太复杂了,网上查了别人的代码..太简单了.. 1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 // Sta ...

  7. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  8. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

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

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

  10. LeetCode(53):Maximum Subarray

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

最新文章

  1. 正值实系数多项式函数所对应的导数累加和是否非负?
  2. Dagger2从入门到熟练
  3. java.util.concurrent.FutureTask 源码
  4. Java 8 - Lambda从兴趣盎然到索然无味
  5. 电路 | 稳压电路设计
  6. 三种编程命名规则:驼峰命名法 (壹)
  7. 【数据结构与算法】之深入解析“TinyURL加密与解密”的求解思路与算法示例
  8. mysql update 行迁移_Oracle行迁移和行链接详解
  9. Linux指令--touch
  10. Select的onchange事件
  11. fio与iometer
  12. EXCEL公式系列:方差、标准差
  13. Word 之 清除页眉下划线
  14. 一句话理解:过拟合和欠拟合
  15. 对结对编程盟友的个人项目代码分析
  16. FPGA开发软件详细清单
  17. 系统检测到您疑似使用网页抓取工具访问本_12款最常使用的网络爬虫工具推荐...
  18. cesium添加自定义点位图片
  19. python.opencv.imread读图顺序:从上到下,从左到右
  20. windows-phone-power-tools [wp 安装部署xap]

热门文章

  1. [书目]软件制胜之道:执行的策略
  2. 这样出ORACLE的面试题
  3. 使用EF操作Oracle数据库小计
  4. Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]【解决方案】
  5. 【前端】react and redux教程学习实践,浅显易懂的实践学习方法。
  6. jsp常见获取地址函数之间的不同
  7. 《拥抱变化——社交网络时代的企业转型之道》一构筑社会信任
  8. 【转】Java代码操作Redis的sentinel和Redis的集群Cluster操作
  9. 1 SQL server数据库基础
  10. EMC助力广东福彩中心容灾系统建设