Skip to content

Commit 4a14e9f

Browse files
add 1154
1 parent 9ed098f commit 4a14e9f

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|5087|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5087.java) | O(1) | O(1) | |Medium||
3131
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
32+
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||
3233
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
3334
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
3435
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | O(n^2) | O(1) | |Easy||
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Calendar;
4+
5+
/**
6+
* 1154. Day of the Year
7+
*
8+
* Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
9+
*
10+
* Example 1:
11+
*
12+
* Input: date = "2019-01-09"
13+
* Output: 9
14+
* Explanation: Given date is the 9th day of the year in 2019.
15+
* Example 2:
16+
*
17+
* Input: date = "2019-02-10"
18+
* Output: 41
19+
* Example 3:
20+
*
21+
* Input: date = "2003-03-01"
22+
* Output: 60
23+
* Example 4:
24+
*
25+
* Input: date = "2004-03-01"
26+
* Output: 61
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* date.length == 10
32+
* date[4] == date[7] == '-', and all other date[i]'s are digits
33+
* date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.
34+
* */
35+
public class _1154 {
36+
public static class Solution1 {
37+
Calendar cal = Calendar.getInstance();
38+
39+
public int dayOfYear(String date) {
40+
int year = Integer.parseInt(date.substring(0, 4));
41+
int month = Integer.parseInt(date.substring(5, 7));
42+
int day = Integer.parseInt(date.substring(8, 10));
43+
int thirtyDays = 30;
44+
int thirtyOneDays = 31;
45+
if (month == 1) {
46+
return day;
47+
} else if (month == 2) {
48+
return day + thirtyOneDays;
49+
} else {
50+
int daysInFeb = isLeapYear(year) ? 29 : 28;
51+
if (month == 3) {
52+
return thirtyOneDays + daysInFeb + day;
53+
} else if (month == 4) {
54+
return 2 * thirtyOneDays + daysInFeb + day;
55+
} else if (month == 5) {
56+
return 2 * thirtyOneDays + daysInFeb + day + thirtyDays;
57+
} else if (month == 6) {
58+
return 3 * thirtyOneDays + daysInFeb + day + thirtyDays;
59+
} else if (month == 7) {
60+
return 3 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays;
61+
} else if (month == 8) {
62+
return 4 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays;
63+
} else if (month == 9) {
64+
return 5 * thirtyOneDays + daysInFeb + day + 2 * thirtyDays;
65+
} else if (month == 10) {
66+
return 5 * thirtyOneDays + daysInFeb + day + 3 * thirtyDays;
67+
} else if (month == 11) {
68+
return 6 * thirtyOneDays + daysInFeb + day + 3 * thirtyDays;
69+
} else {
70+
return 6 * thirtyOneDays + daysInFeb + day + 4 * thirtyDays;
71+
}
72+
}
73+
}
74+
75+
private boolean isLeapYear(int year) {
76+
cal.set(Calendar.YEAR, year);
77+
return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
78+
}
79+
}
80+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1154;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1154Test {
10+
private static _1154.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1154.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(9, solution1.dayOfYear("2019-01-09"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(271, solution1.dayOfYear("1969-09-28"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy