Skip to content

Commit 6ab0c5a

Browse files
committed
update: 添加问题“3280.将日期转换为二进制表示”的代码和题解
1 parent 3435204 commit 6ab0c5a

11 files changed

+341
-209
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-01 18:43:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-01 18:48:09
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
string ten2two(string s) {
14+
int a = stoi(s);
15+
string temp = bitset<32>(a).to_string();
16+
return temp.substr(temp.find('1'));
17+
}
18+
public:
19+
string convertDateToBinary(string date) {
20+
return ten2two(date.substr(0, 4)) + '-' + ten2two(date.substr(5, 2)) + '-' + ten2two(date.substr(8, 2));
21+
}
22+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-01 18:54:58
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-01 18:57:15
6+
*/
7+
package main
8+
9+
import (
10+
"strconv"
11+
"strings"
12+
)
13+
14+
func convertDateToBinary(date string) string {
15+
a := strings.Split(date, "-")
16+
for i := range a {
17+
x, _ := strconv.Atoi(a[i])
18+
a[i] = strconv.FormatUint(uint64(x), 2)
19+
}
20+
return strings.Join(a, "-")
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-01 18:51:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-01 18:52:45
6+
*/
7+
class Solution {
8+
public String convertDateToBinary(String date) {
9+
String[] a = date.split("-");
10+
for (int i = 0; i < a.length; i++) {
11+
a[i] = Integer.toBinaryString(Integer.parseInt(a[i]));
12+
}
13+
return String.join("-", a);
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-01-01 18:49:00
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-01-01 18:50:23
6+
'''
7+
class Solution:
8+
def convertDateToBinary(self, date: str) -> str:
9+
return '-'.join(bin(int(s))[2:] for s in date.split('-'))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-01 18:36:02
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-01 18:41:24
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
string ten2two(string original) {
14+
int ten = 0;
15+
for (char c : original) {
16+
ten = ten * 10 + c - '0';
17+
}
18+
string ans;
19+
while (ten) {
20+
ans = char(ten % 2 + '0') + ans;
21+
ten >>= 1;
22+
}
23+
return ans;
24+
}
25+
public:
26+
string convertDateToBinary(string date) {
27+
return ten2two(date.substr(0, 4)) + '-' + ten2two(date.substr(5, 2)) + '-' + ten2two(date.substr(8, 2));
28+
}
29+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@
753753
|3259.超级饮料的最大强化能量|中等|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/01/LeetCode%203259.%E8%B6%85%E7%BA%A7%E9%A5%AE%E6%96%99%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BC%BA%E5%8C%96%E8%83%BD%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143429899" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/2973620/letmefly-3259chao-ji-yin-liao-de-zui-da-eeusa/" target="_blank">LeetCode题解</a>|
754754
|3264.K次乘运算后的最终数组I|简单|<a href="https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/13/LeetCode%203264.K%E6%AC%A1%E4%B9%98%E8%BF%90%E7%AE%97%E5%90%8E%E7%9A%84%E6%9C%80%E7%BB%88%E6%95%B0%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144442552" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/solutions/3018990/letmefly-3264k-ci-cheng-yun-suan-hou-de-ahxh2/" target="_blank">LeetCode题解</a>|
755755
|3266.K次乘运算后的最终数组II|困难|<a href="https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/14/LeetCode%203266.K%E6%AC%A1%E4%B9%98%E8%BF%90%E7%AE%97%E5%90%8E%E7%9A%84%E6%9C%80%E7%BB%88%E6%95%B0%E7%BB%84II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144473973" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-ii/solutions/3020166/letmefly-3266k-ci-cheng-yun-suan-hou-de-44xf1/" target="_blank">LeetCode题解</a>|
756+
|3280.将日期转换为二进制表示|简单|<a href="https://leetcode.cn/problems/convert-date-to-binary/solutions/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144870892" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/convert-date-to-binary/solutions/3036032/letmefly-3280jiang-ri-qi-zhuan-huan-wei-149mv/" target="_blank">LeetCode题解</a>|
756757
|3285.找到稳定山的下标|简单|<a href="https://leetcode.cn/problems/find-indices-of-stable-mountains/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/19/LeetCode%203285.%E6%89%BE%E5%88%B0%E7%A8%B3%E5%AE%9A%E5%B1%B1%E7%9A%84%E4%B8%8B%E6%A0%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144596618" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-indices-of-stable-mountains/solutions/3025068/letmefly-3285zhao-dao-wen-ding-shan-de-x-u3wc/" target="_blank">LeetCode题解</a>|
757758
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
758759
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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>&nbsp;</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>&nbsp;</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)

newSolution.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2024-11-01 12:57:48
5+
LastEditTime: 2025-01-01 18:35:18
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
'''
@@ -11,6 +11,7 @@
1111
import sys
1212
import time
1313
import datetime
14+
import subprocess
1415
from urllib.parse import quote
1516

1617

@@ -203,4 +204,56 @@ def getProblemUrl():
203204
print(prResult)
204205
prNumber = int(prResult.split('/')[-1])
205206
os.system(f'gh pr merge {prNumber} -m -d')
206-
os.system(f'cd OtherSource/gitcode_knowledge && git pull && git push Let main:From_GitCode_CSDN')
207+
# https://github.com/LetMeFly666/LeetCode/blob/3435204860a8a85aa666618d90f40916dc70a1f1/reassign.py
208+
def syncGitcodeCSDN():
209+
nowCWD = os.getcwd()
210+
os.chdir('OtherSource/gitcode_knowledge')
211+
os.system('git pull')
212+
# 判断一个commit是否按配置签名
213+
def verify_commit(commit_hash: str) -> bool:
214+
result = subprocess.run(
215+
['git', 'verify-commit', commit_hash],
216+
stdout=subprocess.PIPE,
217+
stderr=subprocess.PIPE,
218+
)
219+
return not result.returncode
220+
# 获取一个commit的上一个commit
221+
def get_parent_commit(commit_hash: str) -> str:
222+
result = subprocess.run(
223+
['git', 'log', commit_hash, '--pretty=%H', '-n', '2'],
224+
stdout=subprocess.PIPE,
225+
stderr=subprocess.PIPE,
226+
)
227+
return result.stdout.decode('utf-8').strip().split()[-1]
228+
# 将从某次commit开始至HEAD的所有commit重新签名
229+
def re_assign(commit_hash: str) -> None:
230+
# cmd = f'git filter-branch -f --commit-filter \'git commit-tree -S "$@";\' {commit_hash}..HEAD'
231+
# print(cmd)
232+
# os.system(cmd)
233+
env = os.environ.copy()
234+
env['FILTER_BRANCH_SQUELCH_WARNING'] = "1"
235+
result = subprocess.run(
236+
["git", "filter-branch", "-f", "--commit-filter", 'git commit-tree -S "$@";', f"{commit_hash}..HEAD"],
237+
env=env,
238+
)
239+
print(result.returncode)
240+
# subprocess.Popen(cmd)
241+
def re_assign_main():
242+
# re_assign('HEAD~2')
243+
# return
244+
if verify_commit('HEAD'): # HEAD的签名也能被验证
245+
return
246+
notVerified = 'HEAD'
247+
while True:
248+
next = get_parent_commit(notVerified)
249+
if next == notVerified:
250+
break
251+
notVerified = next
252+
if verify_commit(next):
253+
break
254+
print(notVerified)
255+
re_assign(notVerified)
256+
os.system('git push --force') # resign gitcode
257+
os.system('git push Let main:From_GitCode_CSDN')
258+
os.chdir(nowCWD)
259+
syncGitcodeCSDN()

0 commit comments

Comments
 (0)