Leetcode-949 Largest Time for Given Digits(给定数字能组成的最大时间)

  1 #define _for(i,a,b) for(int i = (b)-1;i >= (a);i --)
  2 class Solution
  3 {
  4     public:
  5         int hash[10] {0};
  6         string result;
  7         int dfs(int pos,int limit)
  8         {
  9             if(pos==4)
 10                 return 1;
 11             else if(pos==0)
 12             {
 13                 _for(i,0,3)
 14                 {
 15                     if(hash[i])
 16                     {
 17                         if(i==2)
 18                         {
 19                             result += '2';
 20                             hash[2] --;
 21                             int st = dfs(1,1);
 22                             if(st)
 23                             return 1;
 24                             result.pop_back();
 25                             hash[2] ++;
 26                         }
 27                         else
 28                         {
 29                             result += i+'0';
 30                             hash[i] --;
 31                             int st = dfs(1,0);
 32                             if(st)
 33                             return 1;
 34                             result.pop_back();
 35                             hash[i] ++;
 36                         }
 37                     }
 38                 }
 39             }
 40             else if(pos==1 && limit==0)
 41             {
 42                 _for(i,0,10)
 43                 {
 44                     if(hash[i])
 45                     {
 46                         result += i+'0';
 47                         hash[i] --;
 48                         int st = dfs(2,0);
 49                         if(st)
 50                             return 1;
 51                         result.pop_back();
 52                         hash[i] ++;
 53                     }
 54                 }
 55             }
 56             else if(pos==1 && limit==1)
 57             {
 58                 _for(i,0,4)
 59                 {
 60                     if(hash[i])
 61                     {
 62                         result += i+'0';
 63                         hash[i] --;
 64                         int st = dfs(2,1);
 65                         if(st)
 66                             return 1;
 67                         result.pop_back();
 68                         hash[i] ++;
 69                     }
 70                 }
 71             }
 72             else if(pos==2)
 73             {
 74                 _for(i,0,6)
 75                 {
 76                     if(hash[i])
 77                     {
 78                         result += i+'0';
 79                         hash[i] --;
 80                         int st = dfs(3,limit);
 81                         if(st)
 82                             return 1;
 83                         result.pop_back();
 84                         hash[i] ++;
 85                     }
 86                 }
 87             }
 88             else if(pos==3)
 89             {
 90                 _for(i,0,10)
 91                 {
 92                     if(hash[i])
 93                     {
 94                         result += i+'0';
 95                         hash[i] --;
 96                         int st = dfs(4,limit);
 97                         if(st)
 98                             return 1;
 99                         result.pop_back();
100                         hash[i] ++;
101                     }
102                 }
103             }
104             return 0;
105         }
106         string largestTimeFromDigits(vector<int>& A)
107         {
108             hash[A[0]] ++;
109             hash[A[1]] ++;
110             hash[A[2]] ++;
111             hash[A[3]] ++;
112             int st = dfs(0,0);
113             if(st)
114             {
115                 string rnt = "";
116                 rnt += result[0];
117                 rnt += result[1];
118                 rnt += ':';
119                 rnt += result[2];
120                 rnt += result[3];
121                 return rnt;
122             }
123             else
124             {
125                 string a = "";
126                 return a;
127             }
128             return result;
129         }
130 };

慌了

原文地址:https://www.cnblogs.com/Asurudo/p/10052880.html