zoukankan      html  css  js  c++  java
  • 剑指offer42题(2)

    在网上查了另外一种方法,其实也算同一种。有一些动态规划的思想。代码如下:

    package com.algorithm04;
    
    public class Algorithm42_2 {
        
        public int maxSumInSubArray(int[] array){
            
            if(array.length<0)
                return 0;
            if(array.length==1)
                return array[0];
            int max,begin,end,len;
            int[] c = new int[100];
            begin = end = 0;
            max = 0;
            len = array.length;
            for(int i = 1 ; i < len ; i++){
                if(c[i-1]>0)
                {
                    c[i] = c[i-1] +array[i];
                }
                else
                {
                    c[i] = array[i];
                    begin = i;
                }
                if(c[i]>max){
                    max = c[i];
                    end = i;
                }
            }
            return max;
        }
        
        
        public static void main(String[] args) {
            
            Algorithm42_2 algorithm42_2 = new Algorithm42_2();
            int[] arrays = new int[]{1,-2,3,10,-4,7,2,-5};
            algorithm42_2.maxSumInSubArray(arrays);
            
        }
        
    }

    参考http://blog.csdn.net/nciaebupt/article/details/8482077  代码。灰常感谢!

  • 相关阅读:
    Ethical Hacking
    Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    食物链 POJ
    Wireless Network POJ
    Candies POJ
    畅通工程再续 HDU
    Jungle Roads HDU
  • 原文地址:https://www.cnblogs.com/CloudStrife/p/7375791.html
Copyright © 2011-2022 走看看