Skip to content

Commit dc3e088

Browse files
committed
feat: add solutions to lc problem: No.1507
No.1507.Reformat Date
1 parent 86a8ee4 commit dc3e088

File tree

7 files changed

+120
-96
lines changed

7 files changed

+120
-96
lines changed

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

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454

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

57-
切分 `date` 字符串,获取对应的 `year`, `month`, `day`,然后拼接起来即可。
57+
**方法一:模拟**
58+
59+
将字符串按空格分割为三个部分,分别为 `day``month``year`,然后拼接为 `YYYY-MM-DD` 的格式。
60+
61+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5862

5963
<!-- tabs:start -->
6064

@@ -65,26 +69,12 @@
6569
```python
6670
class Solution:
6771
def reformatDate(self, date: str) -> str:
68-
months = [
69-
"Jan",
70-
"Feb",
71-
"Mar",
72-
"Apr",
73-
"May",
74-
"Jun",
75-
"Jul",
76-
"Aug",
77-
"Sep",
78-
"Oct",
79-
"Nov",
80-
"Dec",
81-
]
82-
mapper = {v: str(k + 1) for k, v in enumerate(months)}
83-
split = date.split(' ')
84-
year = split[2]
85-
month = mapper.get(split[1])
86-
day = split[0][: len(split[0]) - 2]
87-
return year + '-' + month.zfill(2) + '-' + day.zfill(2)
72+
s = date.split()
73+
s.reverse()
74+
months = " JanFebMarAprMayJunJulAugSepOctNovDec"
75+
s[1] = str(months.index(s[1]) // 3 + 1).zfill(2)
76+
s[2] = s[2][:-2].zfill(2)
77+
return "-".join(s)
8878
```
8979

9080
### **Java**
@@ -94,21 +84,45 @@ class Solution:
9484
```java
9585
class Solution {
9686
public String reformatDate(String date) {
97-
Map<String, Integer> mapper = new HashMap<>();
98-
String[] months
99-
= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
100-
for (int i = 0; i < months.length; ++i) {
101-
mapper.put(months[i], i + 1);
102-
}
103-
String[] split = date.split(" ");
104-
int year = Integer.parseInt(split[2]);
105-
int month = mapper.get(split[1]);
106-
int day = Integer.parseInt(split[0].substring(0, split[0].length() - 2));
107-
return String.format("%d-%02d-%02d", year, month, day);
87+
var s = date.split(" ");
88+
String months = " JanFebMarAprMayJunJulAugSepOctNovDec";
89+
int day = Integer.parseInt(s[0].substring(0, s[0].length() - 2));
90+
int month = months.indexOf(s[1]) / 3 + 1;
91+
return String.format("%s-%02d-%02d", s[2], month, day);
10892
}
10993
}
11094
```
11195

96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
string reformatDate(string date) {
102+
string months = " JanFebMarAprMayJunJulAugSepOctNovDec";
103+
stringstream ss(date);
104+
string year, month, t;
105+
int day;
106+
ss >> day >> t >> month >> year;
107+
month = to_string(months.find(month) / 3 + 1);
108+
return year + "-" + (month.size() == 1 ? "0" + month : month) + "-" + (day > 9 ? "" : "0") + to_string(day);
109+
}
110+
};
111+
```
112+
113+
### **Go**
114+
115+
```go
116+
func reformatDate(date string) string {
117+
s := strings.Split(date, " ")
118+
day, _ := strconv.Atoi(s[0][:len(s[0])-2])
119+
months := " JanFebMarAprMayJunJulAugSepOctNovDec"
120+
month := strings.Index(months, s[1])/3 + 1
121+
year, _ := strconv.Atoi(s[2])
122+
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
123+
}
124+
```
125+
112126
### **...**
113127

114128
```

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

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,48 +58,58 @@
5858
```python
5959
class Solution:
6060
def reformatDate(self, date: str) -> str:
61-
months = [
62-
"Jan",
63-
"Feb",
64-
"Mar",
65-
"Apr",
66-
"May",
67-
"Jun",
68-
"Jul",
69-
"Aug",
70-
"Sep",
71-
"Oct",
72-
"Nov",
73-
"Dec",
74-
]
75-
mapper = {v: str(k + 1) for k, v in enumerate(months)}
76-
split = date.split(' ')
77-
year = split[2]
78-
month = mapper.get(split[1])
79-
day = split[0][: len(split[0]) - 2]
80-
return year + '-' + month.zfill(2) + '-' + day.zfill(2)
61+
s = date.split()
62+
s.reverse()
63+
months = " JanFebMarAprMayJunJulAugSepOctNovDec"
64+
s[1] = str(months.index(s[1]) // 3 + 1).zfill(2)
65+
s[2] = s[2][:-2].zfill(2)
66+
return "-".join(s)
8167
```
8268

8369
### **Java**
8470

8571
```java
8672
class Solution {
8773
public String reformatDate(String date) {
88-
Map<String, Integer> mapper = new HashMap<>();
89-
String[] months
90-
= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
91-
for (int i = 0; i < months.length; ++i) {
92-
mapper.put(months[i], i + 1);
93-
}
94-
String[] split = date.split(" ");
95-
int year = Integer.parseInt(split[2]);
96-
int month = mapper.get(split[1]);
97-
int day = Integer.parseInt(split[0].substring(0, split[0].length() - 2));
98-
return String.format("%d-%02d-%02d", year, month, day);
74+
var s = date.split(" ");
75+
String months = " JanFebMarAprMayJunJulAugSepOctNovDec";
76+
int day = Integer.parseInt(s[0].substring(0, s[0].length() - 2));
77+
int month = months.indexOf(s[1]) / 3 + 1;
78+
return String.format("%s-%02d-%02d", s[2], month, day);
9979
}
10080
}
10181
```
10282

83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
string reformatDate(string date) {
89+
string months = " JanFebMarAprMayJunJulAugSepOctNovDec";
90+
stringstream ss(date);
91+
string year, month, t;
92+
int day;
93+
ss >> day >> t >> month >> year;
94+
month = to_string(months.find(month) / 3 + 1);
95+
return year + "-" + (month.size() == 1 ? "0" + month : month) + "-" + (day > 9 ? "" : "0") + to_string(day);
96+
}
97+
};
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
func reformatDate(date string) string {
104+
s := strings.Split(date, " ")
105+
day, _ := strconv.Atoi(s[0][:len(s[0])-2])
106+
months := " JanFebMarAprMayJunJulAugSepOctNovDec"
107+
month := strings.Index(months, s[1])/3 + 1
108+
year, _ := strconv.Atoi(s[2])
109+
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
110+
}
111+
```
112+
103113
### **...**
104114

105115
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
string reformatDate(string date) {
4+
string months = " JanFebMarAprMayJunJulAugSepOctNovDec";
5+
stringstream ss(date);
6+
string year, month, t;
7+
int day;
8+
ss >> day >> t >> month >> year;
9+
month = to_string(months.find(month) / 3 + 1);
10+
return year + "-" + (month.size() == 1 ? "0" + month : month) + "-" + (day > 9 ? "" : "0") + to_string(day);
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func reformatDate(date string) string {
2+
s := strings.Split(date, " ")
3+
day, _ := strconv.Atoi(s[0][:len(s[0])-2])
4+
months := " JanFebMarAprMayJunJulAugSepOctNovDec"
5+
month := strings.Index(months, s[1])/3 + 1
6+
year, _ := strconv.Atoi(s[2])
7+
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
8+
}
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
class Solution {
22
public String reformatDate(String date) {
3-
Map<String, Integer> mapper = new HashMap<>();
4-
String[] months
5-
= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
6-
for (int i = 0; i < months.length; ++i) {
7-
mapper.put(months[i], i + 1);
8-
}
9-
String[] split = date.split(" ");
10-
int year = Integer.parseInt(split[2]);
11-
int month = mapper.get(split[1]);
12-
int day = Integer.parseInt(split[0].substring(0, split[0].length() - 2));
13-
return String.format("%d-%02d-%02d", year, month, day);
3+
var s = date.split(" ");
4+
String months = " JanFebMarAprMayJunJulAugSepOctNovDec";
5+
int day = Integer.parseInt(s[0].substring(0, s[0].length() - 2));
6+
int month = months.indexOf(s[1]) / 3 + 1;
7+
return String.format("%s-%02d-%02d", s[2], month, day);
148
}
159
}
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
class Solution:
22
def reformatDate(self, date: str) -> str:
3-
months = [
4-
"Jan",
5-
"Feb",
6-
"Mar",
7-
"Apr",
8-
"May",
9-
"Jun",
10-
"Jul",
11-
"Aug",
12-
"Sep",
13-
"Oct",
14-
"Nov",
15-
"Dec",
16-
]
17-
mapper = {v: str(k + 1) for k, v in enumerate(months)}
18-
split = date.split(' ')
19-
year = split[2]
20-
month = mapper.get(split[1])
21-
day = split[0][: len(split[0]) - 2]
22-
return year + '-' + month.zfill(2) + '-' + day.zfill(2)
3+
s = date.split()
4+
s.reverse()
5+
months = " JanFebMarAprMayJunJulAugSepOctNovDec"
6+
s[1] = str(months.index(s[1]) // 3 + 1).zfill(2)
7+
s[2] = s[2][:-2].zfill(2)
8+
return "-".join(s)

solution/1500-1599/1513.Number of Substrings With Only 1s/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858

5959
**方法一:遍历计数**
6060

61-
我们遍历字符串 $s$,用变量 $cnt$ 记录当前连续的 1 的个数,用变量 $ans$ 记录答案。当遍历到字符 $s[i]$ 时,如果 $s[i] = 1$,则 $cnt$ 自增 1,否则 $cnt$ 置 0。此时$ans$ 自增 $cnt$。
61+
我们遍历字符串 $s$,用变量 $cnt$ 记录当前连续的 1 的个数,用变量 $ans$ 记录答案。当遍历到字符 $s[i]$ 时,如果 $s[i] = 1$,则 $cnt$ 自增 1,否则 $cnt$ 置 0。此时 $ans$ 自增 $cnt$。
6262

63-
最后,返回 $ans$ 即可。
63+
遍历结束,返回 $ans$ 即可。
6464

6565
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
6666

0 commit comments

Comments
 (0)