|
| 1 | +--- |
| 2 | +title: 3280.将日期转换为二进制表示 |
| 3 | +date: 2025-01-01 18:59:51 |
| 4 | +tags: [题解, LeetCode, 简单, 数学, 字符串, 进制转换] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】3280.将日期转换为二进制表示:库函数实现或手动转换 |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/convert-date-to-binary/](https://leetcode.cn/problems/convert-date-to-binary/) |
| 10 | + |
| 11 | +<p>给你一个字符串 <code>date</code>,它的格式为 <code>yyyy-mm-dd</code>,表示一个公历日期。</p> |
| 12 | + |
| 13 | +<p><code>date</code> 可以重写为二进制表示,只需要将年、月、日分别转换为对应的二进制表示(不带前导零)并遵循 <code>year-month-day</code> 的格式。</p> |
| 14 | + |
| 15 | +<p>返回 <code>date</code> 的 <strong>二进制</strong> 表示。</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | + |
| 19 | +<p><strong class="example">示例 1:</strong></p> |
| 20 | + |
| 21 | +<div class="example-block"> |
| 22 | +<p><strong>输入:</strong> <span class="example-io">date = "2080-02-29"</span></p> |
| 23 | + |
| 24 | +<p><strong>输出:</strong> <span class="example-io">"100000100000-10-11101"</span></p> |
| 25 | + |
| 26 | +<p><strong>解释:</strong></p> |
| 27 | + |
| 28 | +<p><span class="example-io">100000100000, 10 和 11101 分别是 2080, 02 和 29 的二进制表示。</span></p> |
| 29 | +</div> |
| 30 | + |
| 31 | +<p><strong class="example">示例 2:</strong></p> |
| 32 | + |
| 33 | +<div class="example-block"> |
| 34 | +<p><strong>输入:</strong> <span class="example-io">date = "1900-01-01"</span></p> |
| 35 | + |
| 36 | +<p><strong>输出:</strong> <span class="example-io">"11101101100-1-1"</span></p> |
| 37 | + |
| 38 | +<p><strong>解释:</strong></p> |
| 39 | + |
| 40 | +<p><span class="example-io">11101101100, 1 和 1 分别是 1900, 1 和 1 的二进制表示。</span></p> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | + |
| 45 | +<p><strong>提示:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>date.length == 10</code></li> |
| 49 | + <li><code>date[4] == date[7] == '-'</code>,其余的 <code>date[i]</code> 都是数字。</li> |
| 50 | + <li>输入保证 <code>date</code> 代表一个有效的公历日期,日期范围从 1900 年 1 月 1 日到 2100 年 12 月 31 日(包括这两天)。</li> |
| 51 | +</ul> |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +## 解题方法:进制转换 |
| 56 | + |
| 57 | +如果手动将一个十进制字符串转换为一个二进制字符串,应如何做? |
| 58 | + |
| 59 | +首先将字符串转为十进制数: |
| 60 | + |
| 61 | +> 十进制数初始值为0,遍历字符串,每次十进制数乘以10并加上当前遍历到的字符对应的数字。 |
| 62 | +
|
| 63 | +接着将十进制数转为二进制字符串: |
| 64 | + |
| 65 | +> 在十进制数不为零时,不断将十进制数对2取模后的结果添加到二进制字符串的头部,然后将十进制数除以2。 |
| 66 | +
|
| 67 | +当然,现在很多主流的编程语言都有库函数以更加方便地实现。 |
| 68 | + |
| 69 | ++ 时间复杂度$O(C)$,其中$C=len(s)=10$ |
| 70 | ++ 空间复杂度$O(C)$ |
| 71 | + |
| 72 | +### AC代码 |
| 73 | + |
| 74 | +#### C++ - 手动转换版本 |
| 75 | + |
| 76 | +```cpp |
| 77 | +/* |
| 78 | + * @Author: LetMeFly |
| 79 | + * @Date: 2025-01-01 18:36:02 |
| 80 | + * @LastEditors: LetMeFly.xyz |
| 81 | + * @LastEditTime: 2025-01-01 18:41:24 |
| 82 | + */ |
| 83 | +class Solution { |
| 84 | +private: |
| 85 | + string ten2two(string original) { |
| 86 | + int ten = 0; |
| 87 | + for (char c : original) { |
| 88 | + ten = ten * 10 + c - '0'; |
| 89 | + } |
| 90 | + string ans; |
| 91 | + while (ten) { |
| 92 | + ans = char(ten % 2 + '0') + ans; |
| 93 | + ten >>= 1; |
| 94 | + } |
| 95 | + return ans; |
| 96 | + } |
| 97 | +public: |
| 98 | + string convertDateToBinary(string date) { |
| 99 | + return ten2two(date.substr(0, 4)) + '-' + ten2two(date.substr(5, 2)) + '-' + ten2two(date.substr(8, 2)); |
| 100 | + } |
| 101 | +}; |
| 102 | +``` |
| 103 | +
|
| 104 | +#### C++ - 库函数版本 |
| 105 | +
|
| 106 | +```cpp |
| 107 | +/* |
| 108 | + * @Author: LetMeFly |
| 109 | + * @Date: 2025-01-01 18:43:56 |
| 110 | + * @LastEditors: LetMeFly.xyz |
| 111 | + * @LastEditTime: 2025-01-01 18:48:09 |
| 112 | + */ |
| 113 | +class Solution { |
| 114 | +private: |
| 115 | + string ten2two(string s) { |
| 116 | + int a = stoi(s); |
| 117 | + string temp = bitset<32>(a).to_string(); |
| 118 | + return temp.substr(temp.find('1')); |
| 119 | + } |
| 120 | +public: |
| 121 | + string convertDateToBinary(string date) { |
| 122 | + return ten2two(date.substr(0, 4)) + '-' + ten2two(date.substr(5, 2)) + '-' + ten2two(date.substr(8, 2)); |
| 123 | + } |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +#### Python |
| 128 | + |
| 129 | +```python |
| 130 | +''' |
| 131 | +Author: LetMeFly |
| 132 | +Date: 2025-01-01 18:49:00 |
| 133 | +LastEditors: LetMeFly.xyz |
| 134 | +LastEditTime: 2025-01-01 18:50:23 |
| 135 | +''' |
| 136 | +class Solution: |
| 137 | + def convertDateToBinary(self, date: str) -> str: |
| 138 | + return '-'.join(bin(int(s))[2:] for s in date.split('-')) |
| 139 | +``` |
| 140 | + |
| 141 | +#### Java |
| 142 | + |
| 143 | +```java |
| 144 | +/* |
| 145 | + * @Author: LetMeFly |
| 146 | + * @Date: 2025-01-01 18:51:17 |
| 147 | + * @LastEditors: LetMeFly.xyz |
| 148 | + * @LastEditTime: 2025-01-01 18:52:45 |
| 149 | + */ |
| 150 | +class Solution { |
| 151 | + public String convertDateToBinary(String date) { |
| 152 | + String[] a = date.split("-"); |
| 153 | + for (int i = 0; i < a.length; i++) { |
| 154 | + a[i] = Integer.toBinaryString(Integer.parseInt(a[i])); |
| 155 | + } |
| 156 | + return String.join("-", a); |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +#### Go |
| 162 | + |
| 163 | +```go |
| 164 | +/* |
| 165 | + * @Author: LetMeFly |
| 166 | + * @Date: 2025-01-01 18:54:58 |
| 167 | + * @LastEditors: LetMeFly.xyz |
| 168 | + * @LastEditTime: 2025-01-01 18:57:15 |
| 169 | + */ |
| 170 | +package main |
| 171 | + |
| 172 | +import ( |
| 173 | + "strconv" |
| 174 | + "strings" |
| 175 | +) |
| 176 | + |
| 177 | +func convertDateToBinary(date string) string { |
| 178 | + a := strings.Split(date, "-") |
| 179 | + for i := range a { |
| 180 | + x, _ := strconv.Atoi(a[i]) |
| 181 | + a[i] = strconv.FormatUint(uint64(x), 2) |
| 182 | + } |
| 183 | + return strings.Join(a, "-") |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/01/01/LeetCode%203280.%E5%B0%86%E6%97%A5%E6%9C%9F%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA/)哦~ |
| 188 | +> |
| 189 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/144870892](https://letmefly.blog.csdn.net/article/details/144870892) |
0 commit comments