-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert Date to Binary.cpp
42 lines (35 loc) · 1010 Bytes
/
Convert Date to Binary.cpp
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
class Solution {
public:
string convertDateToBinary(string date) {
int i =0;
string ans = "";
while(i<date.length()){
int j = i;
string temp = "";
while(j<date.length() && date[j]!='-') temp+=date[j] , j++;
int x = stoi(temp);
string y = "";
while(x > 0){
y+= to_string(x%2);
x/=2;
}
i = j+1;
string y2 = "";
reverse(y.begin(),y.end());
ans += y + '-';
}
string ans2 = "";
int k = 0;
while(k < ans.length()-1){
int b = k;
string x = "";
while(b<ans.length()-1 && ans[b]=='0') b++;
while(b<ans.length()-1 && ans[b]!='-') x+=ans[b++];
ans2+=x;
ans2+='-';
k = b+1;
}
ans2.pop_back();
return ans2;
}
};