java 376. Wiggle Subsequence.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 376. Wiggle Subsequence.java相关的知识,希望对你有一定的参考价值。

class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) is 1:
            return 1
        cnt, maxi = 1, 0
        flag = None
        for i in range(1, len(nums)):
            diff = nums[i] - nums[i - 1]
            if diff != 0 and flag is None:
                flag = diff > 0
                cnt = 2
            elif flag and diff < 0 or not flag and diff > 0:
                cnt += 1
                flag = not flag
            maxi = max(maxi, cnt)
        return maxi
public class Solution {
    public int wiggleMaxLength(int[] nums) {
        if (nums == null || nums.length < 1) return 0;
        int high = 1;
        int low = 1;
        int len = nums.length;
        for (int i = 1; i < len; i++) {
            if (nums[i] > nums[i - 1]) high = low + 1;
            else if (nums[i] < nums[i - 1]) low = high + 1;
        }
        return Math.max(high, low);
        
    }
}

以上是关于java 376. Wiggle Subsequence.java的主要内容,如果未能解决你的问题,请参考以下文章

java 376. Wiggle Subsequence.java

java 376. Wiggle Subsequence.java

java 376. Wiggle Subsequence.java

java 376. Wiggle Subsequence.java

376. Wiggle Subsequence

[leetcode-376-Wiggle Subsequence]