首页 > 代码库 > javaScript 连续子数列最大和

javaScript 连续子数列最大和

 1 <!DOCTYPE html> 2 <html> 3 <head> 4     <title></title> 5     <meta charset=utf-8> 6     <script> 7         var a=[-1,2,4,-3,5]; 8         var maxSum=0,currentSum=0; 9         for(var i=0;i<a.length;i++){10             currentSum+=a[i];11             if(currentSum<0){12                 currentSum=0;13             }14             if(currentSum>maxSum){15                 maxSum=currentSum;16             }17         }18         console.log(maxSum);19     </script>20 </head>21 <body>22 23 </body>24 </html>

 

javaScript 连续子数列最大和