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

边栏推荐
- Ffmpeg mix
- Redis persistence principle
- GFS distributed file system (it's nice to meet it alone)
- Small program animation realizes the running lantern and animation object
- Dive into deep learning - 2.1 data operation & Exercise
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
- 2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
- BMZCTF simple_ pop
- [software testing-6] & Test Management
- After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
猜你喜欢

SSM based campus part-time platform for College Students

Two drawing interfaces - 1 Matlab style interface

P35-P41 fourth_ context

Redis persistence principle

data2vec! New milestone of unified mode

The latest activation free version of Omni toolbox

After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me

Preliminary cognition of C language pointer

Pdf editing tool movavi pdfchef 2022 direct download

Internationalization and localization, dark mode and dark mode in compose
随机推荐
[set theory] set concept and relationship (set family | set family examples | multiple sets)
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
540. Single element in ordered array
[free completion] development of course guidance platform (source code +lunwen)
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
PostgreSQL database high availability Patroni source code learning - etcd class
[Thesis Writing] how to write the overall design of JSP tourism network
FuncS sh file not found when using the benchmarksql tool to test kingbases
金仓数据库KingbaseES 插件kdb_date_function
Ffmpeg mix
有道云笔记
2022 P cylinder filling test content and P cylinder filling simulation test questions
FFMpeg example
Which code editor is easy to use? Code editing software recommendation
FISCO bcos zero knowledge proof Fiat Shamir instance source code
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
C primre plus Chapter 10 question 6 inverted array
怎么用Kotlin去提高生产力:Kotlin Tips
Function introduction of member points mall system