[leetcode]Next Closest Time

Posted 阿牧遥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]Next Closest Time相关的知识,希望对你有一定的参考价值。

其实暴力也能解,因为就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的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode]Next Closest Time

[LeetCode] Next Closest Time 下一个最近时间点

681. Next Closest Time

Next Closest Time

681. Next Closest Time(Accepted)

DOM——遍历.parent().parents().closest()和.next()