Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
判断一列数是否单调不减或单调不增(也就是,可以有重复数),简单模拟?


 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

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理