-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path949.cpp
More file actions
65 lines (64 loc) · 1.43 KB
/
949.cpp
File metadata and controls
65 lines (64 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
public:
int get_int(vector<int>& A){
int x = 0;
for(int a: A){
x = x*10 + a;
}
return x;
}
string get_string(int x){
string s = to_string(x);
int n = s.size();
string res = "";
if(n==4){
res += s[0];
res += s[1];
res += ':';
res += s[2];
res += s[3];
}
else if(n==3){
res += '0';
res += s[0];
res += ':';
res += s[1];
res += s[2];
}
else if(n==2){
res += '0';
res += '0';
res += ':';
res += s[0];
res += s[1];
}
else{
res += '0';
res += '0';
res += ':';
res += '0';
res += s[0];
}
return res;
}
bool check(int x){
int hour = x/100;
int min = x%100;
return (hour<=23 && hour >=0 && min<=59 && min>=00);
}
string largestTimeFromDigits(vector<int>& A) {
int res = 0;
sort(A.begin(),A.end());
bool flag = false;
do{
int x = get_int(A);
if(res<=x && check(x)){
flag = true;
res = x;
}
}while(next_permutation(A.begin(), A.end()));
if(!flag)
return "";
return get_string(res);
}
};