<div class="question-title" style="box-sizing: border-box; padding-bottom: 10px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); margin-top: 1em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;"><h3 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 0px; margin-bottom: 10px; font-size: 24px; display: inline-block; margin-right: 0.5em;">209. Minimum Size Subarray Sum</h3> <span class="fa fa-lg fa-star add-favor not-favor" data-toggle="popover" data-trigger="hover" data-placement="top" data-content="save to favorite" ng-click="detailCtrl.favor()" ng-class="{'favor': detailCtrl.is_favor, 'not-favor': !detailCtrl.is_favor}" style="box-sizing: border-box; display: inline-block; line-height: 0.75em; font-family: FontAwesome; font-size: 1.33333333em; text-rendering: auto; -webkit-font-smoothing: antialiased; vertical-align: -15%; color: rgb(128, 128, 128); cursor: pointer; position: relative; top: -3px;"></span><a target=_blank class="pull-right btn btn-default" href="https://leetcode.com/problems/minimum-size-subarray-sum/submissions/" style="box-sizing: border-box; background-image: none; color: rgb(51, 51, 51); text-decoration: none; display: inline-block; padding: 6px 12px; margin-bottom: 0px; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; float: right !important; background-position: 0px 0px;">My Submissions</a><div class="pull-right btn-group right-pad" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle; float: right !important; margin-right: 15px;"><button type="button" class="btn btn-default active" style="margin: 0px; font-family: inherit; font-size: 14px; font-style: inherit; font-variant: inherit; line-height: 1.42857143; color: rgb(51, 51, 51); overflow: visible; cursor: pointer; padding: 6px 12px; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; background-image: none; border: 1px solid rgb(173, 173, 173); border-top-left-radius: 4px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 4px; background-color: rgb(230, 230, 230); outline: 0px; -webkit-box-shadow: rgba(0, 0, 0, 0.121569) 0px 3px 5px inset; box-shadow: rgba(0, 0, 0, 0.121569) 0px 3px 5px inset; position: relative; float: left; z-index: 2;">Question</button><button type="button" class="btn btn-default " data-toggle="tooltip" data-placement="top" title="" data-original-title="Not available yet, please check back later." style="margin: 0px 0px 0px -1px; font-family: inherit; font-size: 14px; font-style: inherit; font-variant: inherit; line-height: 1.42857143; color: rgb(51, 51, 51); overflow: visible; cursor: pointer; padding: 6px 12px; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; background-image: none; border: 1px solid rgb(204, 204, 204); border-top-left-radius: 0px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 0px; background-color: rgb(255, 255, 255); position: relative; float: left;">Editorial Solution</button></div></div><div class="row col-md-12" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1140px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;"><span class="total-ac text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); font-size: 13px; vertical-align: text-bottom;">Total Accepted: <span style="box-sizing: border-box; font-weight: 700;">40966</span></span> <span class="total-submit text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); margin-left: 15px; font-size: 13px; vertical-align: text-bottom;">Total Submissions: <span style="box-sizing: border-box; font-weight: 700;">150450</span></span> <span class="total-submit text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); margin-left: 15px; font-size: 13px; vertical-align: text-bottom;">Difficulty: <span style="box-sizing: border-box; font-weight: 700;">Medium</span></span></div><div class="row" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;"><div class="col-md-12" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1170px;"><div class="question-content" style="box-sizing: border-box; margin-left: 20px; margin-top: 20px; margin-bottom: 5px; line-height: 30px; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding-left: 20px; padding-bottom: 2px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Given an array of <span style="box-sizing: border-box; font-weight: 700;">n</span> positive integers and a positive integer <span style="box-sizing: border-box; font-weight: 700;">s</span>, find the minimal length of a subarray of which the sum ≥ <span style="box-sizing: border-box; font-weight: 700;">s</span>. If there isn't one, return 0 instead.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">For example, given the array <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">[2,3,1,2,4,3]</code> and <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">s = 7</code>,<br style="box-sizing: border-box;" />the subarray <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">[4,3]</code> has the minimal length under the problem constraint.</p></div></div></div>
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums==null||nums.length==0){return 0;}
int i=0,j=0;
int len=nums.length;
int result=len;
int sum=0;
while(sum<s&&j<len)
{
sum+=nums[j];
j++;
}
j--; //先加起来,一直加到大于给定的数,加完了还不大于S,则返回0
if(j+1==nums.length)
{
if(sum<s) return 0;
else
{
while(sum-nums[i]>=s)
{
sum=sum-nums[i];
i++;
}
}
}
result=j-i+1;
while(j<len)
{
if(j+1<len&&nums[j+1]>nums[i])
{
sum=sum-nums[i]+nums[j+1];
i++;
while(sum-nums[i]>=s&&i<len&&j+1<len)
{
sum=sum-nums[i];
i++;
}
result=Math.min(result,j+1-i+1);
j++;
}
else
{
if(j+1==len){return Math.min(result,j-i+1);}
else
{
i=j+1;j++;sum=0;
}
}
}
return result;
}
}
以上是我写的代码,整体来说就是不分思想是对的,但是边边角角处都是补丁式的编程,这说明我的全局功底还是不够,所以会出现各种窘况,在自己被自己绕进去之后,想出来就很难了。
public int minSubArrayLen(int s, int[] nums) {
if(nums==null||nums.length==0){return 0;}
int len=nums.length;
int i=0,j=0;
int sum=nums[0];
int result=len;
while(i<len&&j<len)
{
while(sum<s&&j<len)
{
j++;
if(j<len)
sum=sum+nums[j];
}
//j--;
if(j>=len-1&&sum<s) return 0;
result=Math.min(result,j-i+1);
while(sum-nums[i]>=s)
{
sum-=nums[i];
i++;
}
result=Math.min(result, j-i+1);
i++;
j++;
if(j<len&&i<len)
sum=sum-nums[i-1]+nums[j];
}
return result;
}
后来我又自己写了个很是复杂的代码,第二个版本的代码相比第一个版本有所改进,起码是可以运行了但是提交后超时,这说明我的代码比较臃肿,感觉替程序想的太多,其实有很多的过程可以合并来完成,或许这就是算法思想的不足。
public int minSubArrayLen(int s, int[] a) {
if (a == null || a.length == 0)
return 0;
int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE;
while (j < a.length) {
sum += a[j++];
while (sum >= s) {
min = Math.min(min, j - i);
sum -= a[i++];
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
最下面这个是AC的代码,就这么几行代码,其实还是我的编程思想有问题。