Skip to content

Commit e62eb43

Browse files
committed
feat: add python and java solutions to leetcode problem: No.1507
1507. Reformat Date
1 parent 826ad67 commit e62eb43

File tree

6 files changed

+76
-9
lines changed

6 files changed

+76
-9
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,16 @@
157157

158158
## 维护者
159159

160-
[Yang Libin](https://github.com/yanglbme): GitHub / Gitee 技术社区 @Doocs 创建者;[@TheAlgorithms](https://github.com/TheAlgorithms) 组织成员
160+
[Yang Libin](https://github.com/yanglbme): GitHub / Gitee 技术社区 @Doocs 创建者;开源组织 [@TheAlgorithms](https://github.com/TheAlgorithms) 核心成员之一
161161

162162
## 加入我们
163163

164164
刷编程题的最大好处就是可以锻炼解决问题的思维能力。相信我,「如何去思考」​ 本身也是一项需要不断学习和练习的技能。非常感谢前微软工程师、现蚂蚁金服技术专家 [@kfstorm](https://github.com/kfstorm) 贡献了本项目的所有 [C# 题解](https://github.com/doocs/leetcode/pull/245)
165165

166166
如果你对本项目感兴趣,并且希望加入我们刷题大军,欢迎随时提交 [PR](https://github.com/doocs/leetcode/pulls)。请参考如下步骤:
167167

168-
1. 将本项目 <kbd>fork</kbd> 到你的个人 GitHub / Gitee 帐户,然后 <kbd>clone</kbd> 到你的本地机器;
169-
1. 对项目做出一些变更,然后使用 git <kbd>add</kbd>、<kbd>commit</kbd>、<kbd>push</kbd> 等命令将你的变更提交到你的远程 GitHub / Gitee 仓库;
168+
1. 将本项目 fork 到你的个人 GitHub / Gitee 帐户,然后 clone 到你的本地机器;
169+
1. 对项目做出一些变更,然后使用 git addcommitpush 等命令将你的变更提交到你的远程 GitHub / Gitee 仓库;
170170
1. 将你的变更以 PR 的形式提交过来,项目的维护人员会在第一时间对你的变更进行 review!
171171
1. 你也可以参考帮助文档「[GitHub](https://help.github.com/cn) / [Gitee](https://gitee.com/help)」了解更多细节。
172172

README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
154154

155155
I'm looking for long-term contributors/partners to this repo! Send me [PRs](https://github.com/doocs/leetcode/pulls) if you're interested! See the following:
156156

157-
1. <kbd>Fork</kbd> [this repository](https://github.com/doocs/leetcode) to your own GitHub account and then <kbd>clone</kbd> it to your local machine.
158-
1. Make some changes to your leetcode repository, then <kbd>add</kbd>, <kbd>commit</kbd> and <kbd>push</kbd> the changes to your remote GitHub repository.
157+
1. Fork [this repository](https://github.com/doocs/leetcode) to your own GitHub account and then clone it to your local machine.
158+
1. Make some changes to your leetcode repository, then push the changes to your remote GitHub repository.
159159
1. Create a pull request with your changes!
160160
1. See [CONTRIBUTING](https://github.com/doocs/.github/blob/main/CONTRIBUTING.md) or [GitHub Help](https://help.github.com/en) for more details.
161161

solution/1500-1599/1507.Reformat Date/README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,45 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
切分 `date` 字符串,获取对应的 `year`, `month`, `day`,然后拼接起来即可。
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
<!-- 这里可写当前语言的特殊实现逻辑 -->
6163

6264
```python
63-
65+
class Solution:
66+
def reformatDate(self, date: str) -> str:
67+
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
68+
mapper = {v: str(k + 1) for k, v in enumerate(months)}
69+
split = date.split(' ')
70+
year = split[2]
71+
month = mapper.get(split[1])
72+
day = split[0][:len(split[0]) - 2]
73+
return year + '-' + month.zfill(2) + '-' + day.zfill(2)
6474
```
6575

6676
### **Java**
6777

6878
<!-- 这里可写当前语言的特殊实现逻辑 -->
6979

7080
```java
71-
81+
class Solution {
82+
public String reformatDate(String date) {
83+
Map<String, Integer> mapper = new HashMap<>();
84+
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
85+
for (int i = 0; i < months.length; ++i) {
86+
mapper.put(months[i], i + 1);
87+
}
88+
String[] split = date.split(" ");
89+
int year = Integer.parseInt(split[2]);
90+
int month = mapper.get(split[1]);
91+
int day = Integer.parseInt(split[0].substring(0, split[0].length() -2));
92+
return String.format("%d-%02d-%02d", year, month, day);
93+
}
94+
}
7295
```
7396

7497
### **...**

solution/1500-1599/1507.Reformat Date/README_EN.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,34 @@
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def reformatDate(self, date: str) -> str:
61+
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
62+
mapper = {v: str(k + 1) for k, v in enumerate(months)}
63+
split = date.split(' ')
64+
year = split[2]
65+
month = mapper.get(split[1])
66+
day = split[0][:len(split[0]) - 2]
67+
return year + '-' + month.zfill(2) + '-' + day.zfill(2)
6068
```
6169

6270
### **Java**
6371

6472
```java
65-
73+
class Solution {
74+
public String reformatDate(String date) {
75+
Map<String, Integer> mapper = new HashMap<>();
76+
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
77+
for (int i = 0; i < months.length; ++i) {
78+
mapper.put(months[i], i + 1);
79+
}
80+
String[] split = date.split(" ");
81+
int year = Integer.parseInt(split[2]);
82+
int month = mapper.get(split[1]);
83+
int day = Integer.parseInt(split[0].substring(0, split[0].length() -2));
84+
return String.format("%d-%02d-%02d", year, month, day);
85+
}
86+
}
6687
```
6788

6889
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public String reformatDate(String date) {
3+
Map<String, Integer> mapper = new HashMap<>();
4+
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
5+
for (int i = 0; i < months.length; ++i) {
6+
mapper.put(months[i], i + 1);
7+
}
8+
String[] split = date.split(" ");
9+
int year = Integer.parseInt(split[2]);
10+
int month = mapper.get(split[1]);
11+
int day = Integer.parseInt(split[0].substring(0, split[0].length() -2));
12+
return String.format("%d-%02d-%02d", year, month, day);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def reformatDate(self, date: str) -> str:
3+
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
4+
mapper = {v: str(k + 1) for k, v in enumerate(months)}
5+
split = date.split(' ')
6+
year = split[2]
7+
month = mapper.get(split[1])
8+
day = split[0][:len(split[0]) - 2]
9+
return year + '-' + month.zfill(2) + '-' + day.zfill(2)

0 commit comments

Comments
 (0)