当前位置:网站首页>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

边栏推荐
- AWS VPC
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
- 金仓数据库KingbaseES 插件kdb_database_link
- Design and implementation of JSP logistics center storage information management system
- [set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- Asp access teaching management system design finished product
- 金仓数据库KingbaseES 插件kdb_exists_expand
- Ffmpeg mix
- 因子选股-打分模型
猜你喜欢

arthas watch 抓取入参的某个字段/属性

Bugku CTF daily question baby_ flag. txt

Smart contract security audit company selection analysis and audit report resources download - domestic article

Fcpx template: sweet memory electronic photo album photo display animation beautiful memory

Function introduction of member points mall system

data2vec! New milestone of unified mode

Two points -leetcode-540 A single element in an ordered array

Database management tool, querious direct download

Library management system based on SSM

A outsourcing boy's mid-2022 summary
随机推荐
2.14 summary
Small program animation realizes the running lantern and animation object
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
2022-02-12 (338. Bit count)
[fxcg] market analysis today
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
Human resource management system based on JSP
[set theory] set concept and relationship (set family | set family examples | multiple sets)
stm32逆向入门
Factor stock selection scoring model
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
MC Layer Target
GFS distributed file system (it's nice to meet it alone)
FuncS sh file not found when using the benchmarksql tool to test kingbases
[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
有道云笔记
redis 持久化原理
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
2022-02-13 (347. Top k high frequency elements)
Reptile exercise 02