首页 > 代码库 > LintCode-Minimum Subarray
LintCode-Minimum Subarray
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。