leetcode 539 & 401

56 篇文章 0 订阅
53 篇文章 0 订阅

之所以这两题放在一起整理,是觉得两道题目很相近,基本思路一样,而且都是跟时间表示相关的类型。

leetcode 539. Minimum Time Difference

题目描述:

Given a list of 24-hour clock time points in “Hour:Minutes” format, find the minimum minutes difference between any two time points in the list.

Example 1:
Input: [“23:59”,”00:00”]
Output: 1
Note:
The number of time points in the given list is at least 2 and won’t exceed 20000.
The input time is legal and ranges from 00:00 to 23:59.

解题思路

对于普通的时间处理,只需要考虑相邻时间差,这个时候很容易的一个想法就是对时间全部排序,按时钟和分钟分别排序,可是这样并不能减少时间差的计算量,观察到本题的时间粒度是分钟,那么可以把所有时间用分钟表示,这样两者的时间差就很容易计算了。
特别注意:24点问题。在24点时间的两侧,时间差的计算是24*60-两者的数字时间差,假设我们得到一个有序的时间序列,那么时间差的最小值就是相邻时间差与首尾时间差(24点的计算)之间的较小数,你思考一下就明白了。
关于排序:本题未限制时间复杂度和空间复杂度,因此排序是可以AC这道题的,相对的,因为24*60不是一个特别大的数,我们可以用一个bool型数组对时间序列进行标记,存在则标记true,这样就得到了一个自动有序的时间序列,时间复杂度为O(n),具体的代码实现如下(C++):

int string2Minute(string minute) {
        int mMinute = 0;
        mMinute = 10*(minute[0]-'0')+(minute[1] - '0');
        return mMinute;
    }

    int findMinDifference(vector<string>& timePoints) {
        bool mark[24*60] = {false};
        for(auto &time:timePoints) {
            auto hour = stoi(time.substr(0, 2));
            auto minute = stoi(time.substr(3,2));
            if(mark[hour*60+minute]) return 0;
            mark[hour*60+minute] = true;
        }

    int first = 24*600,last = -1;
    int minVal = 24*60;
    int pre = 0;
    for(int i =0;i<24*60;++i) {
        if(mark[i]) {

            if(first!=24*600) {
                minVal = min(minVal,i-pre);
            }
            first = min(first, i);
            pre = i;
            last = max(last, i);

        }
    }
    minVal = min(minVal,24*60-last+first);
    return minVal;
    }

leetcode 401 binary watch

题目描述

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

Each LED represents a zero or one, with the least significant bit on the right.
3:25

For example, the above binary watch reads “3:25”.

Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.

Example:

Input: n = 1
Return: [“1:00”, “2:00”, “4:00”, “8:00”, “0:01”, “0:02”, “0:04”, “0:08”, “0:16”, “0:32”]
Note:
The order of output does not matter.
The hour must not contain a leading zero, for example “01:00” is not valid, it should be “1:00”.
The minute must be consist of two digits and may contain a leading zero, for example “10:2” is not valid, it should be “10:02”.

解题思路

跟上题类似,也是时间相关的题目,所以我们的解决方案也类似,因为最多是24:00,时间粒度也是分钟,只需要扫描所有的分钟序列,并统计出现的1的个数就可以解了。
时钟二进制和分钟二进制位的处理:时钟位有4个二进制数,分钟位有6个,那么可以认为有10个二进制位,前4位代表时钟,后6位代表分钟,时钟位数字左移6位,和分钟位相或,就相当于用一个10位二进制数表示了所有的可能性,我们统计这个10位二进制数的1的个数,就可以找到AC解。
代码如下(C++ 11):

   vector<string> readBinaryWatch(int num) {
       vector<string> retString;
       for(int m = 0;m<12;m++)
        for(int n = 0;n<60;n++) {
            if(bitset<10>(m<<6|n).count()==num){
                retString.push_back(n<10?to_string(m)+":"+string("0")+to_string(n):to_string(m)+":"+to_string(n));
            }
        }
        return retString;
    }

这里用了C++11的stl函数 to_string。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值