896.单调数列

python有对数组进行排序的函数sort,我们只需判断数组和排序后的数组是否相同即可。另外如果原数组是降序的话,那么就是把排序后的数组倒序,比较是否相同。

class Solution(object):
    def isMonotonic(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        # python有对数组进行排序的函数sort,我们只需判断数组和排序后的数组是否相同即可。
        # 另外如果原数组是降序的话,那么就是把排序后的数组倒序,比较是否相同。

        # print(sorted(A)[::-1]) #[3, 2, 1]

        return sorted(A)==A or sorted(A)[::-1]==A

猜你喜欢

转载自blog.csdn.net/weixin_31866177/article/details/85561473