681. Next Closest Time

Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Example 1:

Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later.  It is not 19:33, because this occurs 23 hours and 59 minutes later.

 

Example 2:

Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

根据旧时间,构造出大于旧时间的最小时间。

class Solution {
    private TreeSet<Integer> set = new TreeSet<>();
    
    public String nextClosestTime(String time) {
        char[] array = time.toCharArray();
        int a = array[0] - '0';
        int b = array[1] - '0';
        int c = array[3] - '0';
        int d = array[4] - '0';
        
        set.add(a);
        set.add(b);
        set.add(c);
        set.add(d);
        
        int n = getNext(d, 9);
        if (n > d) return new StringBuilder().append(a).append(b).append(":").append(c).append(n).toString();
        d = n;
        n = getNext(c, 5);
        if (n > c) return new StringBuilder().append(a).append(b).append(":").append(n).append(d).toString();
        c = n;
        
        if (a == 2) {
            n = getNext(b, 3);
        } else {
            n = getNext(b, 9);
        }
        if (n > b) return new StringBuilder().append(a).append(n).append(":").append(c).append(d).toString();
        b = n;
        n = getNext(a, 2);
        return new StringBuilder().append(n).append(b).append(":").append(c).append(d).toString();
    }
    
    private int getNext(int n, int max) {
        Integer next = set.higher(n);
        if (next != null && next <= max) return next;
        return set.iterator().next();
    }
}

input:"19:34"

基于treeset做的,从最小位,也就是分钟的个位,尝试寻找大于此值的最小值。大于4的最小值,也就是9。

如果没找到,那么就用4位数字中的最小值代替当前值。

继续向前尝试,也就是分钟的十位。

对于4位数字,只要有一个值找到了大于他的最小值,那么就结束。

举个栗子,input:"13:55"。

分钟的个位,大于5的最小值,没有,用1替代,十位也一样。=>13:11

小时的个位,找到大于3的最小值,5,替换一下。=>15:11

结束。

 

以下是solution:

class Solution {
    public String nextClosestTime(String time) {
        int cur = 60 * Integer.parseInt(time.substring(0, 2));
        cur += Integer.parseInt(time.substring(3));
        Set<Integer> allowed = new HashSet();
        for (char c: time.toCharArray()) if (c != ':') {
            allowed.add(c - '0');
        }

        while (true) {
            cur = (cur + 1) % (24 * 60);
            int[] digits = new int[]{cur / 60 / 10, cur / 60 % 10, cur % 60 / 10, cur % 60 % 10};
            search : {
                for (int d: digits) if (!allowed.contains(d)) break search;
                return String.format("%02d:%02d", cur / 60, cur % 60);
            }
        }
    }
}

想法也很特别,算出原始时间的分钟数,然后每次加一分钟,看一下是否能由旧时间构成。

java8写法,学习一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值