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,如果是一個非遞減數(shù)組,比較每一個nums[i]nums[i+1],如果出現(xiàn)超過一次nums[i]>nums[i+1],則其不是一個有序的非遞減數(shù)組,由于要考慮循環(huán)問題,因此要比較nums的開頭和結(jié)尾元素。

  • 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/
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容