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

边栏推荐
- Prefix and (continuously updated)
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- Matplotlib -- save graph
- C Primer Plus Chapter 10, question 14 3 × 5 array
- [dynamic programming] subsequence problem
- Some information about the developer environment in Chengdu
- 2022-02-12 (338. Bit count)
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- C language series - Section 3 - functions
- Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
猜你喜欢

BMZCTF simple_ pop

使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错

Solve BP Chinese garbled code

消息队列(MQ)介绍

Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.

After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed

Introduction of pointer variables in function parameters

Employee attendance management system based on SSM

2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
随机推荐
Xrandr modifier la résolution et le taux de rafraîchissement
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
Design and implementation of JSP logistics center storage information management system
解决bp中文乱码
Feature_selection
Interface in TS
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
Small program animation realizes the running lantern and animation object
Summary of training competition (Lao Li's collection of questions)
AWS VPC
Redis persistence principle
stm32逆向入门
[BMZCTF-pwn] 20-secret_ file
Mount NFS in kubesphere
What's wrong with SD card data damage? How to recover SD card data damage
Factor stock selection scoring model
How to use kotlin to improve productivity: kotlin tips
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
[NLP]—sparse neural network最新工作简述
Web - Information Collection