Skip to content

Commit 2b8f4ec

Browse files
authored
Merge pull request #682 from LetMeFly666/3280
添加问题“3280.将日期转换为二进制表示”的代码和题解
2 parents cd2f160 + 6ab0c5a commit 2b8f4ec

11 files changed

+391
-6
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)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ tags: [其他, 知识, 英语, Notes]
915915
|deform|v. 改变...的外形,损毁...的形状,使成畸形|
916916
|||
917917
|denominate|v. 以...(某种货币)为单位,将...命名为,称...为|
918+
|||
918919

919920
<p class="wordCounts">单词收录总数</p>
920921

Solutions/Other-Github-How2Make1VerifiedCommit.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Github - 如何提交一个带有“verified”标识的commit
2+
title: Github - 如何提交一个带有“verified”标识的commit/如何验证一次commit的签名
33
date: 2024-12-28 22:09:55
44
tags: [其他, Github]
55
---
66

7-
# Github - 如何提交一个带有“verified”标识的commit
7+
# Github - 如何提交一个带有“verified”标识的commit/如何验证一次commit的签名
88

99
## 前言(Why)
1010

@@ -22,7 +22,7 @@ tags: [其他, Github]
2222
>
2323
> 验证记录包含一个时间戳,用于标记验证完成的时间。 此持久记录可确保已验证状态的一致性,为存储库内的参与提供稳定的历史记录。 可以通过将鼠标悬停在 GitHub 上的“Verified”徽章上,或者通过[REST API](https://docs.github.com/zh/rest/commits/commits)访问提交(其中包含一个 verified_at 字段)来查看此时间戳。
2424
25-
## How
25+
## How 2 make 1 verify
2626

2727
因为SSH密钥对还可以当<span title="验证身份用">Authentication Key</span>,所以这里介绍如何使用SSH密钥对对一次commit签名(~~其实是因为我**暂时**不太了解GPG Key~~)。
2828

@@ -40,6 +40,52 @@ git config user.signingkey <ssh公钥路径> # 配置ssh公钥路径
4040

4141
在本地进行一次commit并push到Github,顺利的话,在Commit历史记录中就能看到Verified标了。
4242

43+
## How 2 verify 1 commit
44+
45+
我对一次commit通过ssh密钥进行了签名,那么我如何**验证**这次签名呢?
46+
47+
首先需要创建一个签名验证文件,例如`C:\Users\{username}\.ssh\allowed_signers`
48+
49+
对于一个ssh公钥(例如`C:\Users\{username}\.ssh\id_rsa.pub`),格式是这样的:
50+
51+
```
52+
ssh-rsa(也有可能是ssh-ed25519等) NzaC1lZDI1NTE5AAAAAC3NzaC1lZDI1NTE5AAAAI...很长一串... [email protected]
53+
```
54+
55+
也就是`加密算法 公钥数据 邮箱`
56+
57+
我们要调换一下顺序为`邮箱 加密算法 公钥数据`并放到`allowed_signers`(或其他)新文件中。
58+
59+
只会在git中配置:
60+
61+
```bash
62+
git config gpg.ssh.allowedSignersFile c:/Users/{username}/.ssh/allowed_signers
63+
```
64+
65+
然后就能通过commit的hash验证了:
66+
67+
```bash
68+
git verify-commit cd2f160fc1e6d91db48b033bd6e5ac08102454ba
69+
```
70+
71+
## 如何对一个commit重新签名
72+
73+
假设你已经配置好了签名方式:
74+
75+
对最新一次提交重新签名:
76+
77+
```bash
78+
git commit --amend -S --no-edit
79+
```
80+
81+
对历史提交重新签名:
82+
83+
```bash
84+
git filter-branch -f --commit-filter 'git commit-tree -S "$@";' <commit-range>
85+
# <commit-range>是重新签名的commit范围,例如HEAD~3..HEAD
86+
# 若是当前分支的所有commit,可将<commit-range>设为HEAD
87+
```
88+
4389
## End
4490

4591
但是注意,git版本大于等于2.41才支持上述配置。

0 commit comments

Comments
 (0)