2025-01-01
题目传送门:3280. 将日期转换为二进制表示 - 力扣(LeetCode)
模拟题,使用Python解决会更加方便一些,先将对应位置的字符串转成int
,然后再做转二进制的操作,最后在将其转成string
类型。
最后实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: string binary(int x) { string s; while (x) { s.push_back('0' + (x & 1)); x >>= 1; } reverse(s.begin(), s.end()); return s; }
string convertDateToBinary(string date) { int year = stoi(date.substr(0, 4)); int month = stoi(date.substr(5, 2)); int day = stoi(date.substr(8, 2)); return binary(year) + "-" + binary(month) + "-" + binary(day); } };
|
2025-01-02
题目传送门:
最后实现代码如下: