做的好的大学生旅行有哪些网站,200平米简约办公室装修,百度联盟app,wordpress事例题目链接#xff1a; https://leetcode.cn/problems/monotonic-array/description/?envTypestudy-plan-v2envIdprogramming-skills 思路#xff1a; 判断是否为单调数组只有两种情况#xff1a; 1.单调递增时 遍历整个数组 若发现有递减的两项 则不为单调数组…题目链接 https://leetcode.cn/problems/monotonic-array/description/?envTypestudy-plan-v2envIdprogramming-skills 思路 判断是否为单调数组只有两种情况 1.单调递增时 遍历整个数组 若发现有递减的两项 则不为单调数组 2.单调递增时 遍历整个数组 若发现有递增的两项 则不为单调数组 代码
class Solution {public boolean isMonotonic(int[] nums) {//有可能是递减数列int flag 1;if(nums[0]nums[nums.length-1]){for(int i 0 ;inums.length-1;i) {if(nums[i1]nums[i]) {flag -1;break;}}}//有可能是递增数列if(nums[0]nums[nums.length-1]) {for(int i 0;inums.length-1;i) {if(nums[i1]nums[i]) {flag -1;break;}}}if(flag1) return true;return false;}
}