Leetcode681. 最近时刻

Every day a Leetcode

题目来源:681.最近时刻

题目描述

给定一个形如 “HH:MM” 表示的时刻,利用当前出现过的数字构造下一个距离当前时间最近的时刻。每个出现数字都可以被无限次使用。

你可以认为给定的字符串一定是合法的。
例如,“01:34” 和 “12:09” 是合法的,“1:34” 和 “12:9” 是不合法的。

样例 1:

输入: "19:34"
输出: "19:39"
解释: 
  从1,9,3,4中选出的下一个最近的时间是19:39,它是五分钟后。
  答案不是19:33,因为它是23小时59分钟后。

解法1:枚举

代码:

class Solution {
public:
    /**
     * @param time: the given time
     * @return: the next closest time
     */
    string nextClosestTime(string &time) {
        // write your code here
        vector<int> number {time[0]-'0', time[1]-'0', time[3]-'0', time[4]-'0'} ;
        vector<int> currTime(4, 0);
        int hour = number[0]*10 + number[1];
        int minute = number[2]*10 + number[3];
        int times = toMinute(hour, minute);
        int bestTime = times;
        dfs(number, 0, currTime, times, bestTime);
        string s = to_string(bestTime/60) + ":" + to_string(bestTime%60);
        char buff[5];
        sprintf(buff, "%02d:%02d", bestTime / 60, bestTime % 60);
        return string(buff);
    }
    void dfs(vector<int> &number, int deep, vector<int>& currTime, int times, int& bestTime){
        if(deep == 4){
            if(currTime[0]*10 + currTime[1] > 23 || currTime[2]*10 + currTime[3] > 59)
                return;
            int currMinutes = toMinute(currTime[0]*10 + currTime[1], currTime[2]*10 + currTime[3]);
            if(diff(times, currMinutes) < diff(times, bestTime)){
                bestTime = currMinutes;
            }
            return;
        }
        for(int i:number){
            currTime[deep] = i;
            dfs(number, deep+1, currTime, times, bestTime);
        }
        return;
    }
    int toMinute(int hour, int minute){
        return hour*60 + minute;
    }
    int diff(int time1, int time2){
        if(time1 == time2)
            return 1440;
        return ((1440 - time1) + time2) % 1440;
    }
};

解法2:暴力

代码:

class Solution {
public:
    string nextClosestTime(string time) {
        set<int> s;
        s.insert(time[0]-'0');
        s.insert(time[1]-'0');
        s.insert(time[3]-'0');
        s.insert(time[4]-'0');
        if(s.size()==1) return time;//数字都一样
        vector<int> num(s.begin(),s.end());
        int i, j, h, m, size = num.size();
        int hour = (time[0]-'0')*10+time[1]-'0';
        int minute = (time[3]-'0')*10+time[4]-'0';
        int minlargeH = 24, minlargeM = 60;//大于其的最小时间
        int minH = 24, minM = 60;//可能的最小时间
        for(i = 0; i < size; i++)
        {
        	for(j = 0; j < size; j++)
        	{
        		h = m = num[i]*10+num[j];
        		minH = minM = min(minH, h);
        		if(h > hour && h < minlargeH)
        			minlargeH = h;
        		if(m > minute && m < minlargeM)
        			minlargeM = m;
        	}
        }//暴力查找所有可能
        if(minlargeM != 60)//有更大的分钟,时钟不变
        	return time.substr(0,3)+ (minlargeM>=10 ? to_string(minlargeM) : "0"+to_string(minlargeM));
        if(minlargeH != 24)//有更大的时钟,+最小分钟
        	return (minlargeH>=10? to_string(minlargeH) : "0"+to_string(minlargeH)) + ":" + (minM>=10? to_string(minM) : "0"+to_string(minM));
        return (minH>=10? to_string(minH) : "0"+to_string(minH)) + ":" + (minM>=10? to_string(minM): "0"+to_string(minM));
        //都没有,取最小时钟+分钟
    }
};
  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值