当前位置:网站首页>Leetcode simple question: check whether the array is sorted and rotated
Leetcode simple question: check whether the array is sorted and rotated
2022-07-03 04:31:00 【·Starry Sea】
subject
Give you an array nums .nums In the source array of , All elements are related to nums identical , But in non decreasing order .
If nums It can rotate several positions by the source array ( Include 0 A place ) obtain , Then return to true ; otherwise , return false .
There may be... In the source array Duplicate item .
Be careful : We call it an array A In rotation x You get an array of the same length after two positions B , When they meet A[i] == B[(i+x) % A.length] , among % For the remainder operation .
Example 1:
Input :nums = [3,4,5,1,2]
Output :true
explain :[1,2,3,4,5] For ordered source arrays .
It can rotate x = 3 A place , Make the new array from the value to 3 Element start of :[3,4,5,1,2] .
Example 2:
Input :nums = [2,1,3,4]
Output :false
explain : The source array cannot be rotated nums .
Example 3:
Input :nums = [1,2,3]
Output :true
explain :[1,2,3] For ordered source arrays .
It can rotate x = 0 A place ( That is, it doesn't rotate ) obtain nums .
Example 4:
Input :nums = [1,1,1]
Output :true
explain :[1,1,1] For ordered source arrays .
Rotation can be obtained at any position nums .
Example 5:
Input :nums = [2,1]
Output :true
explain :[1,2] For ordered source arrays .
It can rotate x = 5 A place , Make the new array from the value to 2 Element start of :[2,1] .
Tips :
1 <= nums.length <= 100
1 <= nums[i] <= 100
source : Power button (LeetCode)
Their thinking
First find the subscript of the smallest element in the array . There may be more than one smallest element , We select an element as the beginning and loop through the entire array to see whether an increasing sequence can be formed if we start with the current minimum . If there is a beginning traversal, it can make the array in increasing order , Then it can be judged that the modified array is true .
class Solution:
def check(self, nums: List[int]) -> bool:
MIN=min(nums)
temp=[]
for i in range(len(nums)):
if nums[i]==MIN:
temp.append(i)
def match(MIN):
for i in range(MIN,MIN+len(nums)-1):
if nums[i%len(nums)]>nums[(i+1)%len(nums)]:
return False
return True
return True if all([not match(i) for i in temp])==False else False

边栏推荐
- Which code editor is easy to use? Code editing software recommendation
- 有道云笔记
- [set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
- Bugku CTF daily question baby_ flag. txt
- Summary of training competition (Lao Li's collection of questions)
- 金仓KFS数据双向同步场景部署
- 金仓数据库KingbaseES 插件kdb_exists_expand
- Why should programmers learn microservice architecture if they want to enter a large factory?
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- How to use kotlin to improve productivity: kotlin tips
猜你喜欢

Design and implementation of JSP logistics center storage information management system

vulnhub HA: Natraj

Jincang KFS data bidirectional synchronization scenario deployment

Youdao cloud notes

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

Internationalization and localization, dark mode and dark mode in compose

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
![[nlp] - brief introduction to the latest work of spark neural network](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[nlp] - brief introduction to the latest work of spark neural network

data2vec! New milestone of unified mode

解决bp中文乱码
随机推荐
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
金仓数据库KingbaseES 插件kdb_database_link
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
[set theory] binary relation (example of binary relation on a | binary relation on a)
vulnhub HA: Natraj
Preliminary cognition of C language pointer
MC Layer Target
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
Factor stock selection scoring model
MongoDB 慢查询语句优化分析策略
What's wrong with SD card data damage? How to recover SD card data damage
金仓数据库KingbaseES 插件kdb_date_function
Basic use of continuous integration server Jenkins
BMZCTF simple_ pop
[fxcg] market analysis today
消息队列(MQ)介绍
Jincang KFS data bidirectional synchronization scenario deployment
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
金仓数据库KingbaseES 插件kdb_exists_expand