Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 76ac4c3

Browse files
committedApr 4, 2025
Add solution #1154
1 parent 598d128 commit 76ac4c3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,154 LeetCode solutions in JavaScript
1+
# 1,155 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -902,6 +902,7 @@
902902
1145|[Binary Tree Coloring Game](./solutions/1145-binary-tree-coloring-game.js)|Medium|
903903
1146|[Snapshot Array](./solutions/1146-snapshot-array.js)|Medium|
904904
1147|[Longest Chunked Palindrome Decomposition](./solutions/1147-longest-chunked-palindrome-decomposition.js)|Hard|
905+
1154|[Day of the Year](./solutions/1154-day-of-the-year.js)|Easy|
905906
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
906907
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
907908
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|

‎solutions/1154-day-of-the-year.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 1154. Day of the Year
3+
* https://leetcode.com/problems/day-of-the-year/
4+
* Difficulty: Easy
5+
*
6+
* Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD,
7+
* return the day number of the year.
8+
*/
9+
10+
/**
11+
* @param {string} date
12+
* @return {number}
13+
*/
14+
var dayOfYear = function(date) {
15+
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
16+
const [year, month, day] = date.split('-').map(Number);
17+
const isLeapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
18+
if (isLeapYear) daysInMonth[1] = 29;
19+
20+
let result = day;
21+
for (let i = 0; i < month - 1; i++) {
22+
result += daysInMonth[i];
23+
}
24+
25+
return result;
26+
};

0 commit comments

Comments
 (0)
Please sign in to comment.