当前位置:网站首页>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
边栏推荐
猜你喜欢
Pyqt control part (II)
Learning practice: comprehensive application of cycle and branch structure (I)
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
[nlp] - brief introduction to the latest work of spark neural network
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
How to retrieve the password for opening word files
FuncS sh file not found when using the benchmarksql tool to test kingbases
会员积分商城系统的功能介绍
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
随机推荐
Joint set search: merge intervals and ask whether two numbers are in the same set
Square root of X
540. Single element in ordered array
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
How to use kotlin to improve productivity: kotlin tips
Basic use of continuous integration server Jenkins
7. Integrated learning
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
MC Layer Target
Busycal latest Chinese version
What functions need to be set after the mall system is built
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
Kingbasees plug-in KDB of Jincang database_ date_ function
因子选股-打分模型
Two points -leetcode-540 A single element in an ordered array
2022-02-12 (338. Bit count)