题目
判断上升和下降,一般是两个循环分别判断,如何一次性判断两个呢?
class Solution {public int longestMonotonicSubarray(int[] nums) {int ans=1;int i =0,n=nums.length;while(i<n-1){if(nums[i+1]==nums[i]) {i++;continue;}int cur = i;boolean f=nums[i+1]>nums[i];i+=2;while(i<n && nums[i]!=nums[i-1] && (nums[i]>nums[i-1])==f){i++;}ans=Math.max(ans,i-cur);i--;}return ans;}}