Leetcode 1752. Check if Array Is Sorted and Rotated

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1752. Check if Array Is Sorted and Rotated相关的知识,希望对你有一定的参考价值。

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Check if Array Is Sorted and Rotated

2. Solution

**解析:**Version 1,如果是一个非递减数组,比较每一个nums[i]nums[i+1],如果出现超过一次nums[i]>nums[i+1],则其不是一个有序的非递减数组,由于要考虑循环问题,因此要比较nums的开头和结尾元素。

  • Version 1
class Solution:
    def check(self, nums: List[int]) -> bool:
        n = len(nums)
        nums.append(nums[0])
        count = 0
        for i in range(n):
            if nums[i] > nums[i+1]:
                count += 1
                if count > 1:
                    return False
        return True

Reference

  1. https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/

以上是关于Leetcode 1752. Check if Array Is Sorted and Rotated的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array

LeetCode --- 1232. Check If It Is a Straight Line 解题报告

LeetCode --- 1232. Check If It Is a Straight Line 解题报告

leetcode1433. Check If a String Can Break Another String

leetcode1433. Check If a String Can Break Another String

leetcode1461. Check If a String Contains All Binary Codes of Size K