Posted on 2023-09-29 18:48
Uriel 阅读(33)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
判断一列数是否单调不减或单调不增(也就是,可以有重复数),简单模拟?
1 #896
2 #Runtime: 777 ms (Beats 50.28%)
3 #Memory: 23.3 MB (Beats 100%)
4
5 class Solution(object):
6 def isMonotonic(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: bool
10 """
11 if len(nums) == 1:
12 return True
13 i = 0
14 while i < len(nums) - 1 and nums[i] == nums[i + 1]:
15 i += 1
16 if i < len(nums) - 1:
17 if nums[i] < nums[i + 1]:
18 while i < len(nums) - 1:
19 if nums[i] > nums[i + 1]:
20 return False
21 i += 1
22 else:
23 while i < len(nums) - 1:
24 if nums[i] < nums[i + 1]:
25 return False
26 i += 1
27 return True