java 之连续子序列最大和问题的四个解法
来源:互联网 发布:Python编程pdf 编辑:程序博客网 时间:2024/06/05 09:57
import java.util.Random;public final class MaxSumTest{ static private int seqStart = 0; static private int seqEnd = -1; /** *ustczyy *zyyjiao@mail.ustc.edu.cn */ public static int maxSubSum1( int [ ] a ) { int maxSum = 0; for( int i = 0; i < a.length; i++ ) for( int j = i; j < a.length; j++ ) { int thisSum = 0; for( int k = i; k <= j; k++ ) thisSum += a[ k ]; if( thisSum > maxSum ) { maxSum = thisSum; seqStart = i; seqEnd = j; } } return maxSum; } /** *ustczyy *zyyjiao@mail.ustc.edu.cn */ public static int maxSubSum2( int [ ] a ) { int maxSum = 0; for( int i = 0; i < a.length; i++ ) { int thisSum = 0; for( int j = i; j < a.length; j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) { maxSum = thisSum; seqStart = i; seqEnd = j; } } } return maxSum; } /** *ustczyy *zyyjiao@mail.ustc.edu.cn */ public static int maxSubSum3( int [ ] a ) { int maxSum = 0; int thisSum = 0; for( int i = 0, j = 0; j < a.length; j++ ) { thisSum += a[ j ]; if( thisSum > maxSum ) { maxSum = thisSum; seqStart = i; seqEnd = j; } else if( thisSum < 0 ) { i = j + 1; thisSum = 0; } } return maxSum; } /** * *ustczyy *zyyjiao@mail.ustc.edu.cn */ private static int maxSumRec( int [ ] a, int left, int right ) { int maxLeftBorderSum = 0, maxRightBorderSum = 0; int leftBorderSum = 0, rightBorderSum = 0; int center = ( left + right ) / 2; if( left == right ) // Base case return a[ left ] > 0 ? a[ left ] : 0; int maxLeftSum = maxSumRec( a, left, center ); int maxRightSum = maxSumRec( a, center + 1, right ); for( int i = center; i >= left; i-- ) { leftBorderSum += a[ i ]; if( leftBorderSum > maxLeftBorderSum ) maxLeftBorderSum = leftBorderSum; } for( int i = center + 1; i <= right; i++ ) { rightBorderSum += a[ i ]; if( rightBorderSum > maxRightBorderSum ) maxRightBorderSum = rightBorderSum; } return max3( maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum ); } /** * Return maximum of three integers. */ private static int max3( int a, int b, int c ) { return a > b ? a > c ? a : c : b > c ? b : c; } /** * ustczyy *zyyjiao@mail.ustc.edu.cn */ public static int maxSubSum4( int [ ] a ) { return a.length > 0 ? maxSumRec( a, 0, a.length - 1 ) : 0; } public static void getTimingInfo( int n, int alg ) { int [] test = new int[ n ]; long startTime = System.currentTimeMillis( );; long totalTime = 0; int i; for( i = 0; totalTime < 4000; i++ ) { for( int j = 0; j < test.length; j++ ) test[ j ] = rand.nextInt( 100 ) - 50; switch( alg ) { case 1: maxSubSum1( test ); break; case 2: maxSubSum2( test ); break; case 3: maxSubSum3( test ); break; case 4: maxSubSum4( test ); break; } totalTime = System.currentTimeMillis( ) - startTime; } System.out.println( "Algorithm #" + alg + "\t" + "N = " + test.length + "\ttime = " + ( totalTime * 1000 / i ) + " microsec" ); } private static Random rand = new Random( ); /** * Simple test program. */ public static void main( String [ ] args ) { int a[ ] = { 4, -3, 5, -2, -1, 2, 6, -2 }; int maxSum; maxSum = maxSubSum1( a ); System.out.println( "Max sum is " + maxSum + "; it goes" + " from " + seqStart + " to " + seqEnd ); maxSum = maxSubSum2( a ); System.out.println( "Max sum is " + maxSum + "; it goes" + " from " + seqStart + " to " + seqEnd ); maxSum = maxSubSum3( a ); System.out.println( "Max sum is " + maxSum + "; it goes" + " from " + seqStart + " to " + seqEnd ); maxSum = maxSubSum4( a ); System.out.println( "Max sum is " + maxSum ); // Get some timing info for( int n = 10; n <= 1000000; n *= 10 ) for( int alg = 4; alg >= 1; alg-- ) { if( alg == 1 && n > 50000 ) continue; getTimingInfo( n, alg ); } }}
0 1
- java 之连续子序列最大和问题的四个解法
- 最大连续子序列最大和的四种解法
- 最大连续子序列和问题O(n)解法
- 关于最大连续子序列问题的解法
- 最大连续子序列和的几种解法
- 总结java最大连续子序列和的问题
- 最大连续子序列和的问题
- [数据结构]连续子序列最大和,最大积的问题
- 求连续子数组的最大和O(n)解法之思路与Java实现
- 2、最大子序列和问题的四个算法
- [珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列
- [珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列
- [珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列
- [珠玑之椟]字符串和序列:左移、哈希、最长重复子序列的后缀数组解法、最大连续子序列
- 连续子序列最大和问题(Java语言)
- 轰炸问题、连续子序列的积、n个最大连续子序列和
- 连续最大子序列的几种解法
- 连续子序列最大和问题
- 关于手机的序列号,UDID,IMEI,IMSI,ICCID详解
- java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened be
- LocalBroadcastManager
- 服务好“最后一公里”,高效CDN架构经验
- WINVNC源码阅读(七)
- java 之连续子序列最大和问题的四个解法
- 如何自定义UISearchBar
- iOS的XML解析器:SHXMLParser
- CSS 背景透明度
- Java 7之传统I/O - 其它相关字节输入输出流类
- Linux sqlite3基本命令
- sprintf用法详解
- android自定义splash界面
- OS X下为Django安装&连接MySQL数据库