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

相关文章:

  • 2021-09-25
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2022-03-04
  • 2022-12-23
猜你喜欢
  • 2021-07-26
  • 2021-07-27
  • 2021-07-09
  • 2021-11-21
  • 2021-09-07
  • 2022-12-23
  • 2022-01-05
相关资源
相似解决方案