算法 贪心也可以用动态规划核心思想 我主要采用的是贪心的做法,对于任意开始,如果前一组数是>,那后一组只要是<即可,不是就换下一个数。代码 class Solution { public int wiggleMaxLength(int[] nums) { if(nums.length == 1) return 1; int total = 0; int sign = 0; for(int i = 1;i < nums.length;i++){ if(sign == 0){ if(nums[i] > nums[i-1]) sign = 1; if(nums[i] < nums[i-1]) sign = -1; if(nums[i] != nums[i-1]) total += 2; else { continue; } }else{ if(sign == 1){ if(nums[i] < nums[i-1]){ sign = -1; total++; }else{ continue; } } else if(sign == -1){ if(nums[i] > nums[i-1]){ sign = 1; total++; }else{ continue; } }else{ continue; } } } if(total == 0) total++; return total; } }