首页 > 其他 > 详细

[leetcode]Next Closest Time

时间:2020-01-31 01:04:39      阅读:91      评论:0      收藏:0      [点我收藏+]

其实暴力也能解,因为就4*4*4*4种

class Solution:
    def findNext(self, char, nums):
        i = 0
        while i < len(nums):
            if nums[i] == char:
                if i != len(nums) - 1:
                    return nums[i + 1]
                else:
                    return None
            i += 1
        return None
    
    def nextClosestTime(self, time: str) -> str:
        nums = []
        for i in [0, 1, 3, 4]:
            if time[i] not in nums:
                nums.append(time[i])
        nums = sorted(nums)
        
        c4 = self.findNext(time[4], nums)
        if c4 and int(time[3] + c4) < 60:
            return time[:4] + c4
        c3 = self.findNext(time[3], nums)
        if c3 and int(c3 + nums[0]) < 60:
            return time[:3] + c3 + nums[0]
        c1 = self.findNext(time[1], nums)
        if c1 and int(time[0] + c1) <= 24:
            return time[0] + c1 + ‘:‘ + nums[0] + nums[0]
        c0 = self.findNext(time[0], nums)
        if c0 and int(c0 + nums[0]) <= 24:
            return c0 + nums[0] + ‘:‘ + nums[0] + nums[0]
        return nums[0] + nums[0] + ‘:‘ + nums[0] + nums[0]
        

  

[leetcode]Next Closest Time

原文:https://www.cnblogs.com/lautsie/p/12244495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!