局部最优——>全局最优&无反例,试试贪心
455.分发饼干
力扣题目链接:. - 力扣(LeetCode)
class Solution {public int findContentChildren(int[] g, int[] s) {Arrays.sort(s);Arrays.sort(g);int gindex=0;int count=0;for(int sindex=0;sindex<s.length&&gindex<g.length;sindex++){if(s[sindex]>=g[gindex]){gindex++;count++;}}return count;}
}
376. 摆动序列
力扣题目链接:. - 力扣(LeetCode)
class Solution {public int wiggleMaxLength(int[] nums) {int preDiff=0;int curDiff=0;int count=1;for(int i=1;i<nums.length;i++){curDiff=nums[i]-nums[i-1];if((curDiff>0&&preDiff<=0)||(curDiff<0&&preDiff>=0)){count++;preDiff=curDiff;}}return count;}
}
53. 最大子序和
力扣题目链接:. - 力扣(LeetCode)
class Solution {public int maxSubArray(int[] nums) {if(nums.length==1){return nums[0];}int sum=Integer.MIN_VALUE;int tempsum=0;for(int i=0;i<nums.length;i++){tempsum+=nums[i];sum=Math.max(sum,tempsum);if(tempsum<0){tempsum=0;}}return sum;}
}