Minimum Size Subarray Sum

原创 2015年07月07日 20:45:28
public class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null || nums.length <1) {
            return 0;
        }
        int ptr1 = 0;
        int ptr2 = 0;
        int sum = nums[0];
        int result = Integer.MAX_VALUE;
        while (ptr2 < nums.length) {
            if (ptr1 == ptr2) {
                if (sum >= s) {
                    return 1;
                }
                ptr2++;
                if (ptr2 < nums.length) {
                    sum += nums[ptr2];
                } else {
                    return result;
                }
            } else {
                if (sum >= s) {
                    result = Math.min(ptr2 - ptr1 + 1, result);
                    sum -= nums[ptr1];
                    ptr1++;                    
                } else {
                    ptr2++;
                    if (ptr2 < nums.length) {
                        sum += nums[ptr2];
                    } else {
                        if (ptr1 == 0) {
                            return 0;
                        } else {
                            return result;
                        }
                    }                
                }
            }
        }
        return result;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Minimum Size Subarray Sum-最小长度字数组和问题

Minimum Size Subarray Sum问题

Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarra...

Minimum Size Subarray Sum

题目: Given an array of n positive integers and a positive integer s, find the minimal length of ...

Leetcode——209. Minimum Size Subarray Sum

题目: Given an array of n positive integers and a positive integer s, find the minimal length of a co...

Leetcode 209 - Minimum Size Subarray Sum(二分 or 双指针)

题意给一个数组a[]和一个数s,要求在a里面找到一个长度最短的连续子序列,并且子序列的和大于等于s。思路算法1前缀和 + 二分。时间复杂度O(nlogn)O(nlogn)。我们先维护一个前缀和ss,然...
  • Lzedo
  • Lzedo
  • 2017-02-12 20:35
  • 117

Minimum Size Subarray Sum - LeetCode 209

题目描述: Given an array of n positive integers and a positive integer s, find the minimal length of a ...

LeetCode 209. Minimum Size Subarray Sum 滑动窗口

一、题目 Given an array of n positive integers and a positive integer s, find the minimal length of a ...

(待进一步研究吃透)209. Minimum Size Subarray Sum

209. Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find...

[Leetcode]Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarr...

【leetcode】209. Minimum Size Subarray Sum

【leetcode】209. Minimum Size Subarray Sum
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)