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.

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”.
Subscribe to see which companies asked this question
解法一:Language-Java Time-O(1) Space-O(n) Run Time-34ms
算法思路:
h*64+m刚好拼成一个“时钟+分钟”的二进制串,其中1的个数=num的时间即为所求的时间
注意:乘法可以用位运算来解决,这样效率更高。例如本题中h*64可以写成h<<6

public class Solution {
    public List<String> readBinaryWatch(int num) {
        List<String> list = new ArrayList<String>();
        for(int h = 0; h < 12; h ++)
        {
            for(int m = 0; m < 60; m ++)
            {
                if(Integer.bitCount(h * 64 + m) == num)//bitCount:返回二进制串中1的个数
                {
                    list.add(String.format("%d:%02d", h, m));
                }
            }
        }
        return list;
    }
}

解法二:Language-Java Time-O(1) Space-O(n) Run Time-35ms
解题思路:
遍历0-0x2FC之间的数,看哪些数的bitCount满足条件,即为所求解

public class Solution {
    public List<String> readBinaryWatch(int num) {
        List<String> res = new ArrayList<String>();
        for(int i = 0; i < 0x2FC; i ++)
        {
            //分钟要小于60
            if(Integer.bitCount(i) == num && (i&0x03F)<60)
            {
               res.add(String.format("%d:%02d", i >> 6, i & 0x03F)); 
            }
        }
        return res;
    }
}
好的,下面是每一个函数方法的详细解释: 1. `def model_xgb(train, test):`:定义了一个名为 `model_xgb` 的函数,该函数接受两个参数 `train` 和 `test`。 2. `params = {...}`:定义了一个字典 `params`,包含了 XGBoost 模型的参数设置。 3. `dtrain = xgb.DMatrix(train.drop(['User_id', 'Coupon_id', 'Date_received', 'label'], axis=1), label=train['label'])`:将训练集数据转换为 DMatrix 格式,其中去掉了 `User_id`、`Coupon_id`、`Date_received` 和 `label` 列,并将 `label` 列作为标签。 4. `dtest = xgb.DMatrix(test.drop(['User_id', 'Coupon_id', 'Date_received'], axis=1))`:将测试集数据转换为 DMatrix 格式,其中去掉了 `User_id`、`Coupon_id` 和 `Date_received` 列。 5. `watchlist = [(dtrain, 'train')]`:定义了一个监控列表,用于监控训练过程中的性能。 6. `model = xgb.train(params, dtrain, num_boost_round=500, evals=watchlist)`:训练 XGBoost 模型,使用了上面定义的参数和监控列表。 7. `predict = model.predict(dtest)`:对测试集进行预测,得到预测结果。 8. `predict = pd.DataFrame(predict, columns=['prob'])`:将预测结果转换为 DataFrame 格式,并将列名设为 `prob`。 9. `result = pd.concat([test[['User_id', 'Coupon_id', 'Date_received']], predict], axis=1)`:将测试集的 `User_id`、`Coupon_id` 和 `Date_received` 列与预测结果合并,得到最终结果。 10. `feat_importance = pd.DataFrame(columns=['feature_name', 'importance'])`:定义一个空的 DataFrame,用于存储特征重要性信息。 11. `feat_importance['feature_name'] = model.get_score().keys()`:将特征重要性信息中的特征名称取出来,并存储到 `feature_name` 列中。 12. `feat_importance['importance'] = model.get_score().values()`:将特征重要性信息中的特征重要性取出来,并存储到 `importance` 列中。 13. `feat_importance.sort_values(['importance'], ascending=False, inplace=True)`:按照特征重要性从大到小排序。 14. `return result, feat_importance`:返回预测结果和特征重要性信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值