Skip to content

Commit a75dee8

Browse files
add 1118
1 parent 390008f commit a75dee8

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ _If you like this project, please leave me a star._ ★
6262
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | |Easy||
6363
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | |Easy||
6464
|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java) | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy||
65+
|1118|[Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1118.java) | |Easy||
6566
|1108|[Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1108.java) | [:tv:](https://www.youtube.com/watch?v=FP0Na-pL0qk)|Easy||
6667
|1103|[Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1103.java) | |Easy|Math|
6768
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1118. Number of Days in a Month
8+
*
9+
* Given a year Y and a month M, return how many days there are in that month.
10+
*
11+
* Example 1:
12+
* Input: Y = 1992, M = 7
13+
* Output: 31
14+
*
15+
* Example 2:
16+
* Input: Y = 2000, M = 2
17+
* Output: 29
18+
*
19+
* Example 3:
20+
* Input: Y = 1900, M = 2
21+
* Output: 28
22+
*
23+
* Note:
24+
* 1583 <= Y <= 2100
25+
* 1 <= M <= 12
26+
* */
27+
public class _1118 {
28+
public static class Solution1 {
29+
public int numberOfDays(int Y, int M) {
30+
int[] map = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
31+
if (isLeapYear(Y) && M == 2) {
32+
return 29;
33+
} else {
34+
return map[M];
35+
}
36+
}
37+
38+
private boolean isLeapYear(int year) {
39+
return year % 4 == 0 && ((year % 100 == 0 && year % 400 == 0) || year % 100 != 0);
40+
}
41+
}
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1118;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1118Test {
10+
private static _1118.Solution1 solution1;
11+
12+
@Before
13+
public void setupForEachTest() {
14+
solution1 = new _1118.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(31, solution1.numberOfDays(1992, 7));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(29, solution1.numberOfDays(2000, 2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(28, solution1.numberOfDays(1900, 2));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(29, solution1.numberOfDays(1836, 2));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)