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

Lintcode: Minimum Subarray

时间:2016-01-09 07:32:10      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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

Return the sum of the subarray.

Have you met this question in a real interview? Yes
Example
For [1, -1, -2, 1], return -3

Note
The subarray should contain at least one integer.

 

 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         // write your code
 8         int local = nums.get(0);
 9         int global = nums.get(0);
10         for (int i=1; i<nums.size(); i++) {
11             local = Math.min(local+nums.get(i), nums.get(i));
12             global = Math.min(local, global);
13         }
14         return global;
15     }
16 }

 

Lintcode: Minimum Subarray

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5115396.html

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