You are given a string date representing a Gregorian calendar date in the yyyy-mm-dd format.
date can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in year-month-day format.
Input: date ="2080-02-29"Output: "100000100000-10-11101"Explanation:
100000100000,10, and 11101 are the binary representations of 2080,02, and 29respectively.
To convert a date string in yyyy-mm-dd format to its binary representation, we split the string into year, month, and day, convert each to an integer, then to binary (without leading zeros), and join them with dashes.
classSolution {
public: string dateToBinary(string date) {
int y = stoi(date.substr(0,4));
int m = stoi(date.substr(5,2));
int d = stoi(date.substr(8,2));
return bitset<16>(y).to_string().substr(bitset<16>(y).to_string().find('1')) +"-"+ bitset<8>(m).to_string().substr(bitset<8>(m).to_string().find('1')) +"-"+ bitset<8>(d).to_string().substr(bitset<8>(d).to_string().find('1'));
}
};
1
2
3
4
5
6
funcDateToBinary(datestring) string {
y, _:=strconv.Atoi(date[:4])
m, _:=strconv.Atoi(date[5:7])
d, _:=strconv.Atoi(date[8:])
returnstrconv.FormatInt(int64(y), 2) +"-"+strconv.FormatInt(int64(m), 2) +"-"+strconv.FormatInt(int64(d), 2)
}
1
2
3
4
5
6
7
8
classSolution {
public String dateToBinary(String date) {
int y = Integer.parseInt(date.substring(0, 4));
int m = Integer.parseInt(date.substring(5, 7));
int d = Integer.parseInt(date.substring(8));
return Integer.toBinaryString(y) +"-"+ Integer.toBinaryString(m) +"-"+ Integer.toBinaryString(d);
}
}
1
2
3
4
5
6
7
8
classSolution {
fundateToBinary(date: String): String {
val y = date.substring(0, 4).toInt()
val m = date.substring(5, 7).toInt()
val d = date.substring(8).toInt()
returnInteger.toBinaryString(y) + "-" + Integer.toBinaryString(m) + "-" + Integer.toBinaryString(d)
}
}
1
2
3
4
classSolution:
defdateToBinary(self, date: str) -> str:
y, m, d = map(int, date.split('-'))
returnf"{bin(y)[2:]}-{bin(m)[2:]}-{bin(d)[2:]}"