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

边栏推荐
- 使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
- [PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
- [set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
- 使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- PostgreSQL database high availability Patroni source code learning - etcd class
- BMZCTF simple_ pop
- JS multidimensional array to one-dimensional array
- Some information about the developer environment in Chengdu
- Priv-app permission异常
猜你喜欢

The latest activation free version of Omni toolbox

Basic use of continuous integration server Jenkins

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

Employee attendance management system based on SSM
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

Golang -- realize file transfer

Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended

金仓KFS数据双向同步场景部署

FISCO bcos zero knowledge proof Fiat Shamir instance source code

智能合约安全审计公司选型分析和审计报告资源下载---国内篇
随机推荐
Drf--- quick start 01
FISCO bcos zero knowledge proof Fiat Shamir instance source code
data2vec! New milestone of unified mode
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
[software testing-6] & Test Management
金仓KFS数据双向同步场景部署
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
因子选股-打分模型
[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
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
IPhone x forgot the boot password
The latest activation free version of Omni toolbox
有道云笔记
[free completion] development of course guidance platform (source code +lunwen)
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
Classes in TS
[BMZCTF-pwn] 20-secret_ file
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
解决bp中文乱码