day31

1、leetcode455 分发饼干

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int gIndex = 0;
        int sIndex = 0;

        while(sIndex < s.length && gIndex < g.length) {
            if(s[sIndex] >= g[gIndex]) {
                sIndex++;
                gIndex++;
            } else {
                sIndex++;
            }
        }

        return gIndex;
    }
}

2、leetcode376 摆动序列

class Solution {
    public int wiggleMaxLength(int[] nums) {
        if(nums.length <= 1) {
            return nums.length;
        }

        int curDiff = 0;
        int preDiff = 0;
        int result = 1;

        for(int i=0; i<nums.length-1; i++) {
            curDiff = nums[i+1] - nums[i];
            if((preDiff<=0 && curDiff>0) || (preDiff>=0 && curDiff<0)) {
                result++;
                preDiff = curDiff;
            }
        }

        return result;

    }
}

3、leetcode53 最大子序和

class Solution {
    public int maxSubArray(int[] nums) {
        int res = Integer.MIN_VALUE;
        int sum = 0;
        
        for(int i=0; i<nums.length; i++) {
            sum += nums[i];
            if(sum > res) {
                res = sum;
            }  
            if(sum <= 0) {
                sum = 0;
            }
        }

        return res;
    }
}
posted @ 2023-02-14 22:46  黄三七  阅读(16)  评论(0)    收藏  举报