401. Binary Watch

10 篇文章 0 订阅

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.

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".
分析: 链接https://discuss.leetcode.com/topic/59309/simple-java-ac-solution-with-explanation

思想:用一个数组vector<int> a(10, 0)表示小时和分钟的各个位。这里a[0]....a[3]分别代表1小时,2小时,4小时,8小时,

a[i]只能取0,1;a[4].....a[9]分别代表分钟的1,2,......32。根据a,就可以得到时间。

有k个灯亮,表明只能从数组a中选取k个树,组成时间,用深度递归。

这里再得到由k个元素组成的时间时,要判断小时在12之内,分在60之内,否则解是无效解。

class Solution {
    //判断时间是否是有效时间
    bool istrue(vector
   
   
    
     & a)
    {
        int min,hour;
        min=hour=0;
        for(int i=0;i<4;i++)
        hour+=(1<
    
    
     
      & a)
    {
        int min,hour;
        min=hour=0;
        for(int i=0;i<4;i++)
        hour+=(1<
     
     
      
       a, vector
      
      
       
        & r, string & s)
   {
       
       if(k==num)//亮的灯==num,返回
       {
           if(istrue(a))
           {
           s=print(a);
           r.push_back(s);
           }
           return;
       }else 
       if(i==a.size())
       return;
       else
       {
           //a[i]只有两种取值0,1
           a[i]=0;
           DFS(i+1,k,num,a,r,s);//a[i]=0,表示灯未亮,k不变
           a[i]=1;
           DFS(i+1,k+1,num,a,r,s);a[i]=1,表示灯亮,k+1,
           //递归结束后,重设a[i]=0
           a[i]=0;
       }
   }
public:
    vector
       
       
         readBinaryWatch(int num) { vector 
        
          r; string s; vector 
         
           a(10,0); DFS(0,0,num,a,r,s); return r; } }; 
          
         
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是每一个函数方法的详细解释: 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、付费专栏及课程。

余额充值