Skip to content

Commit 36dfb13

Browse files
add 1360
1 parent d5d2fda commit 36dfb13

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1360|[Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1360.java) | |Easy||
1112
|1358|[Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1358.java) | |Medium|String|
1213
|1357|[Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1357.java) | |Medium|Design|
1314
|1356|[Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1356.java) | |Easy|Sort, Bit Manipulation|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1360. Number of Days Between Two Dates
5+
*
6+
* Write a program to count the number of days between two dates.
7+
* The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
8+
*
9+
* Example 1:
10+
* Input: date1 = "2019-06-29", date2 = "2019-06-30"
11+
* Output: 1
12+
*
13+
* Example 2:
14+
* Input: date1 = "2020-01-15", date2 = "2019-12-31"
15+
* Output: 15
16+
*
17+
* Constraints:
18+
* The given dates are valid dates between the years 1971 and 2100.
19+
* */
20+
public class _1360 {
21+
public static class Solution1 {
22+
public int daysBetweenDates(String date1, String date2) {
23+
String[] strings1 = date1.split("-");
24+
String[] strings2 = date2.split("-");
25+
return Math.abs(julianDay(Integer.parseInt(strings1[0]), Integer.parseInt(strings1[1]), Integer.parseInt(strings1[2])) -
26+
julianDay(Integer.parseInt(strings2[0]), Integer.parseInt(strings2[1]), Integer.parseInt(strings2[2])));
27+
}
28+
29+
public int julianDay(int year, int month, int day) {
30+
int a = (14 - month) / 12;
31+
int y = year + 4800 - a;
32+
int m = month + 12 * a - 3;
33+
int jdn = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
34+
return jdn;
35+
}
36+
}
37+
}
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._1360;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1360Test {
10+
private static _1360.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1360.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.daysBetweenDates("2019-06-29", "2019-06-30"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(15, solution1.daysBetweenDates("2020-01-15", "2019-12-31"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)