From 2604e22296b3d0e67f3110b4afa4266ab05cf5be Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 31 Dec 2023 09:13:51 +0800 Subject: [PATCH] feat: add ts solution to lc problem: No.1154 (#2169) No.1154.Day of the Year --- .../1100-1199/1154.Day of the Year/README.md | 25 +++++++++++++------ .../1154.Day of the Year/README_EN.md | 23 ++++++++++++----- .../1154.Day of the Year/Solution.cpp | 5 ++-- .../1154.Day of the Year/Solution.go | 5 ++-- .../1154.Day of the Year/Solution.ts | 8 ++++++ 5 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 solution/1100-1199/1154.Day of the Year/Solution.ts diff --git a/solution/1100-1199/1154.Day of the Year/README.md b/solution/1100-1199/1154.Day of the Year/README.md index d13b1caea0365..64e638a496a15 100644 --- a/solution/1100-1199/1154.Day of the Year/README.md +++ b/solution/1100-1199/1154.Day of the Year/README.md @@ -50,7 +50,7 @@ 最后,根据给定的日期计算出当年的第几天,即把前面每个月的天数累加起来,再加上当月的天数即可。 -时间复杂度为 $O(1)$,空间复杂度为 $O(1)$。 +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -94,9 +94,8 @@ class Solution { class Solution { public: int dayOfYear(string date) { - int y = stoi(date.substr(0, 4)); - int m = stoi(date.substr(5, 2)); - int d = stoi(date.substr(8)); + int y, m, d; + sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d); int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28; int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int ans = d; @@ -112,9 +111,8 @@ public: ```go func dayOfYear(date string) (ans int) { - y, _ := strconv.Atoi(date[:4]) - m, _ := strconv.Atoi(date[5:7]) - d, _ := strconv.Atoi(date[8:]) + var y, m, d int + fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d) days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} if y%400 == 0 || (y%4 == 0 && y%100 != 0) { days[1] = 29 @@ -127,6 +125,19 @@ func dayOfYear(date string) (ans int) { } ``` +### **TypeScript** + +```ts +function dayOfYear(date: string): number { + const y = +date.slice(0, 4); + const m = +date.slice(5, 7); + const d = +date.slice(8); + const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28; + const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + return days.slice(0, m - 1).reduce((a, b) => a + b, d); +} +``` + ### **JavaScript** ```js diff --git a/solution/1100-1199/1154.Day of the Year/README_EN.md b/solution/1100-1199/1154.Day of the Year/README_EN.md index 3669e530799ae..73b40c74701fa 100644 --- a/solution/1100-1199/1154.Day of the Year/README_EN.md +++ b/solution/1100-1199/1154.Day of the Year/README_EN.md @@ -85,9 +85,8 @@ class Solution { class Solution { public: int dayOfYear(string date) { - int y = stoi(date.substr(0, 4)); - int m = stoi(date.substr(5, 2)); - int d = stoi(date.substr(8)); + int y, m, d; + sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d); int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28; int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int ans = d; @@ -103,9 +102,8 @@ public: ```go func dayOfYear(date string) (ans int) { - y, _ := strconv.Atoi(date[:4]) - m, _ := strconv.Atoi(date[5:7]) - d, _ := strconv.Atoi(date[8:]) + var y, m, d int + fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d) days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} if y%400 == 0 || (y%4 == 0 && y%100 != 0) { days[1] = 29 @@ -118,6 +116,19 @@ func dayOfYear(date string) (ans int) { } ``` +### **TypeScript** + +```ts +function dayOfYear(date: string): number { + const y = +date.slice(0, 4); + const m = +date.slice(5, 7); + const d = +date.slice(8); + const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28; + const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + return days.slice(0, m - 1).reduce((a, b) => a + b, d); +} +``` + ### **JavaScript** ```js diff --git a/solution/1100-1199/1154.Day of the Year/Solution.cpp b/solution/1100-1199/1154.Day of the Year/Solution.cpp index bb7a6bf1e8a37..f23496a0d8ece 100644 --- a/solution/1100-1199/1154.Day of the Year/Solution.cpp +++ b/solution/1100-1199/1154.Day of the Year/Solution.cpp @@ -1,9 +1,8 @@ class Solution { public: int dayOfYear(string date) { - int y = stoi(date.substr(0, 4)); - int m = stoi(date.substr(5, 2)); - int d = stoi(date.substr(8)); + int y, m, d; + sscanf(date.c_str(), "%d-%d-%d", &y, &m, &d); int v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28; int days[] = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int ans = d; diff --git a/solution/1100-1199/1154.Day of the Year/Solution.go b/solution/1100-1199/1154.Day of the Year/Solution.go index af5157fe3da31..f9360b2d4d459 100644 --- a/solution/1100-1199/1154.Day of the Year/Solution.go +++ b/solution/1100-1199/1154.Day of the Year/Solution.go @@ -1,7 +1,6 @@ func dayOfYear(date string) (ans int) { - y, _ := strconv.Atoi(date[:4]) - m, _ := strconv.Atoi(date[5:7]) - d, _ := strconv.Atoi(date[8:]) + var y, m, d int + fmt.Sscanf(date, "%d-%d-%d", &y, &m, &d) days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} if y%400 == 0 || (y%4 == 0 && y%100 != 0) { days[1] = 29 diff --git a/solution/1100-1199/1154.Day of the Year/Solution.ts b/solution/1100-1199/1154.Day of the Year/Solution.ts new file mode 100644 index 0000000000000..4bb5d58ac3f58 --- /dev/null +++ b/solution/1100-1199/1154.Day of the Year/Solution.ts @@ -0,0 +1,8 @@ +function dayOfYear(date: string): number { + const y = +date.slice(0, 4); + const m = +date.slice(5, 7); + const d = +date.slice(8); + const v = y % 400 == 0 || (y % 4 == 0 && y % 100) ? 29 : 28; + const days = [31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + return days.slice(0, m - 1).reduce((a, b) => a + b, d); +}