Lintcode最小子数组
2017-10-03 12:18
288 查看
给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。
注意事项
子数组最少包含一个数字
您在真实的面试中是否遇到过这个题?
Yes
样例
给出数组[1, -1, -2, 1],返回 -3
public class Solution {
/*
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(List<Integer> nums) {
// write your code here
if(nums==null){
return 0;
}
int sum=0;
int min=nums.get(0);
for(int i=0;i<nums.size();i++){
sum+=nums.get(i);
if(sum<min){
min=sum;
}
if(sum>0){
sum=0;
}
}
return min;
}
}
注意事项
子数组最少包含一个数字
您在真实的面试中是否遇到过这个题?
Yes
样例
给出数组[1, -1, -2, 1],返回 -3
public class Solution {
/*
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(List<Integer> nums) {
// write your code here
if(nums==null){
return 0;
}
int sum=0;
int min=nums.get(0);
for(int i=0;i<nums.size();i++){
sum+=nums.get(i);
if(sum<min){
min=sum;
}
if(sum>0){
sum=0;
}
}
return min;
}
}
相关文章推荐
- LintCode: 和大于S的最小子数组
- lintcode-44-最小子数组
- Lintcode-贪心-最大子数组和最小子数组
- LintCode_044_最小子数组
- LintCode_044_最小子数组
- LintCode:M-和大于S的最小子数组
- lintcode最小子数组
- LintCode--最小子数组
- lintcode 容易题:Minimum Subarray 最小子数组
- LintCode: 最小子数组
- lintcode编程题 java实现:(一)和大于S的最小子数组
- lintcode 44.最小子数组
- 排颜色问题——数组 leetcode lintcode
- lintcode求两个数组的交出错
- lintcode: 把排序数组转换为高度最小的二叉搜索树
- LintCode-39. 恢复旋转排序数组
- LintCode:M-搜索旋转排序数组
- LintCode:两数组的交
- lintcode,寻找旋转排序数组中的最小值
- lintcode刷题--连续子数组求和