bool isMonotonic(int* nums, int numsSize) {int flag = 2;for (int i = 1; i < numsSize; i++) {if (nums[i-1] > nums[i]) {if (flag == 0)return false;flag = 1;}else if (nums[i-1] < nums[i]) {if (flag == 1)return false;flag = 0;}}return true;
}
此代码较为简单,但在算法思想方面值得借鉴