码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode-Minimum Subarray

时间:2015-01-01 00:08:45      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

Given an array of integers, find the subarray with smallest sum.

Return the sum of the subarray.

Note

The subarray should contain at least one integer.

Example

For [1, -1, -2, 1], return -3

Solution:

 1 public class Solution {
 2     /**
 3      * @param nums: a list of integers
 4      * @return: A integer indicate the sum of minimum subarray
 5      */
 6     public int minSubArray(ArrayList<Integer> nums) {
 7         int len = nums.size();
 8         if (len==0) return 0;
 9 
10         int curMin = nums.get(0);
11         int minRes = nums.get(0);
12 
13         for (int i=1;i<len;i++){
14             curMin = Math.min(nums.get(i),curMin+nums.get(i));
15             minRes = Math.min(curMin,minRes);
16         }
17         return minRes;
18     }
19 }

 

LintCode-Minimum Subarray

标签:

原文地址:http://www.cnblogs.com/lishiblog/p/4196837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!