赞
踩
class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int res = 0; for(int i =0,j=0 ; i<g.length && j<s.length;){ if(s[j]>=g[i]){ res++; i++; j++; }else{ j++; } } return res; } }
prediff > 0
curdiff <0
prediff = 0
curdiff <0
** **此时也算作摆动prediff
curdiff
,其中 prediff 初始化为 0
,curdiff
初始化为 1
三种结果收集情况
prediff<=0 && curdiff>0
收集结果 ,谷点prediff>=0 && curdiff<0
收集结果 ,峰点prediff
,只在有坡度变化的情况下更新 prediff
class Solution { public int wiggleMaxLength(int[] nums) { int prediff = 0; int curdiff = 0; int res = 1; for(int i = 0 ; i < nums.length-1;i++){ curdiff = nums[i+1] - nums[i]; if(prediff<=0 && curdiff>0){ res++; prediff = curdiff; }else if(prediff >=0 && curdiff<0){ res++; prediff = curdiff; } } return res; } }
res = 0
class Solution {
public int maxSubArray(int[] nums) {
int sum = 0;
int res = Integer.MIN_VALUE;
for(int i = 0 ; i < nums.length;i++){
sum+=nums[i];
if(sum>=res){
res = sum;
}
if(sum<0) sum=0;
}
return res;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。