Skip to content

Commit 2604e22

Browse files
authored
feat: add ts solution to lc problem: No.1154 (#2169)
No.1154.Day of the Year
1 parent af3c3c7 commit 2604e22

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

solution/1100-1199/1154.Day of the Year/README.md

+18-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
5151
最后,根据给定的日期计算出当年的第几天,即把前面每个月的天数累加起来,再加上当月的天数即可。
5252

53-
时间复杂度为 $O(1)$,空间复杂度为 $O(1)$。
53+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5454

5555
<!-- tabs:start -->
5656

@@ -94,9 +94,8 @@ class Solution {
9494
class Solution {
9595
public:
9696
int dayOfYear(string date) {
97-
int y = stoi(date.substr(0, 4));
98-
int m = stoi(date.substr(5, 2));
99-
int d = stoi(date.substr(8));
97+
int y, m, d;
98+
sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d);
10099
int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
101100
int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
102101
int ans = d;
@@ -112,9 +111,8 @@ public:
112111
113112
```go
114113
func dayOfYear(date string) (ans int) {
115-
y, _ := strconv.Atoi(date[:4])
116-
m, _ := strconv.Atoi(date[5:7])
117-
d, _ := strconv.Atoi(date[8:])
114+
var y, m, d int
115+
fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d)
118116
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
119117
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
120118
days[1] = 29
@@ -127,6 +125,19 @@ func dayOfYear(date string) (ans int) {
127125
}
128126
```
129127

128+
### **TypeScript**
129+
130+
```ts
131+
function dayOfYear(date: string): number {
132+
const y = +date.slice(0, 4);
133+
const m = +date.slice(5, 7);
134+
const d = +date.slice(8);
135+
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
136+
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
137+
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
138+
}
139+
```
140+
130141
### **JavaScript**
131142

132143
```js

solution/1100-1199/1154.Day of the Year/README_EN.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ class Solution {
8585
class Solution {
8686
public:
8787
int dayOfYear(string date) {
88-
int y = stoi(date.substr(0, 4));
89-
int m = stoi(date.substr(5, 2));
90-
int d = stoi(date.substr(8));
88+
int y, m, d;
89+
sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d);
9190
int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
9291
int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
9392
int ans = d;
@@ -103,9 +102,8 @@ public:
103102
104103
```go
105104
func dayOfYear(date string) (ans int) {
106-
y, _ := strconv.Atoi(date[:4])
107-
m, _ := strconv.Atoi(date[5:7])
108-
d, _ := strconv.Atoi(date[8:])
105+
var y, m, d int
106+
fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d)
109107
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
110108
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
111109
days[1] = 29
@@ -118,6 +116,19 @@ func dayOfYear(date string) (ans int) {
118116
}
119117
```
120118

119+
### **TypeScript**
120+
121+
```ts
122+
function dayOfYear(date: string): number {
123+
const y = +date.slice(0, 4);
124+
const m = +date.slice(5, 7);
125+
const d = +date.slice(8);
126+
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
127+
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
128+
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
129+
}
130+
```
131+
121132
### **JavaScript**
122133

123134
```js

solution/1100-1199/1154.Day of the Year/Solution.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Solution {
22
public:
33
int dayOfYear(string date) {
4-
int y = stoi(date.substr(0, 4));
5-
int m = stoi(date.substr(5, 2));
6-
int d = stoi(date.substr(8));
4+
int y, m, d;
5+
sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d);
76
int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
87
int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
98
int ans = d;

solution/1100-1199/1154.Day of the Year/Solution.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
func dayOfYear(date string) (ans int) {
2-
y, _ := strconv.Atoi(date[:4])
3-
m, _ := strconv.Atoi(date[5:7])
4-
d, _ := strconv.Atoi(date[8:])
2+
var y, m, d int
3+
fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d)
54
days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
65
if y%400 == 0 || (y%4 == 0 && y%100 != 0) {
76
days[1] = 29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function dayOfYear(date: string): number {
2+
const y = +date.slice(0, 4);
3+
const m = +date.slice(5, 7);
4+
const d = +date.slice(8);
5+
const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28;
6+
const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
7+
return days.slice(0, m - 1).reduce((a, b) => a + b, d);
8+
}

0 commit comments

Comments
 (0)