Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 자바
- Matrix
- Binary Search
- SQL
- database
- geometry
- 코테
- Class
- java
- Math
- two pointers
- Method
- Tree
- hash table
- 코딩테스트
- dynamic programming
- greedy
- Number Theory
- Stack
- 구현
- array
- implement
- 파이썬
- bit manipulation
- Counting
- sorting
- simulation
- string
- Data Structure
- Binary Tree
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1360. Number of Days Between Two Dates 본문
1. Input
1) String date1
2) String date2
2. Output
1) date1과 date2의 날짜 수 차이를 반환
3. Constraint
1) 주어지는 입력은 1971년부터 2100년 사이의 날짜이다.
4. Example
Input: date1 = "2019-06-29", date2 = "2019-06-30" -> Output: 1
Input: date1 = "2020-01-15", date2 = "2019-12-31" -> Output: 15
5. Code
1) 첫 코드(2023/04/11)
// 메인
int y1 = Integer.valueOf(date1.substring(0,4));
int y2 = Integer.valueOf(date2.substring(0,4));
int m1 = Integer.valueOf(date1.substring(5,7));
int m2 = Integer.valueOf(date2.substring(5,7));
int d1 = Integer.valueOf(date1.substring(8));
int d2 = Integer.valueOf(date2.substring(8));
return Math.abs(countdays(y1,m1,d1)-countdays(y2,m2,d2));
static int countdays(int year, int month, int day){
int[] md = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i=0 ; i<month-1 ; i++)
day += md[i];
for(int i=1971 ; i<year ; i++){
day += 365;
if(i%4==0){
if(i%100!=0) day++;
else if(i%400==0) day++;
}
}
if(month>2){
if(year%4==0){
if(year%100!=0) day++;
else if(year%400==0) day++;
}
}
return day;
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1417. Reformat The String (0) | 2023.04.12 |
---|---|
[LeetCode/Easy] 1374. Generate a String With Characters That Have Odd Counts (0) | 2023.04.11 |
[LeetCode/Medium] 2390. Removing Stars From a String (0) | 2023.04.11 |
[LeetCode/Easy] 1304. Find N Unique Integers Sum up to Zero (0) | 2023.04.11 |
[LeetCode/Easy] 1309. Decrypt String from Alphabet to Integer Mapping (0) | 2023.04.11 |