题目链接
分析
水题水做
按题意用 $bitset$ 转二进制输出即可
代码实现
class Solution {
public:
string to_binary(int x) {
bitset<16> bs(x);
return bs.to_string().substr(bs.to_string().find_first_of('1'));
}
string convertDateToBinary(string date) {
return to_binary(stoi(date.substr(0, 4))) + "-" +
to_binary(stoi(date.substr(5, 2))) + "-" +
to_binary(stoi(date.substr(8, 2)));
}
};
复杂度分析
- 时间复杂度:$O(1)$
- 空间复杂度:$O(1)$