-
72. Day of the YearProblem Solving 2023. 1. 14. 19:51728x90
https://leetcode.com/problems/day-of-the-year/
Day of the Year - LeetCode
Day of the Year - Given a string date representing a Gregorian calendar [https://en.wikipedia.org/wiki/Gregorian_calendar] date formatted as YYYY-MM-DD, return the day number of the year. Example 1: Input: date = "2019-01-09" Output: 9 Explanation: Give
leetcode.com
YYYY-MM-DD를 입력받고 그 해의 몇 번째 날인지 찾기
class Solution: def dayOfYear(self, date: str) -> int: year, month, day = map(int, date.split('-')) if month == 1: answer = day elif month == 2: answer = 31 + day elif month == 3: answer = 31 + 28 + day elif month == 4: answer = 31 + 28 + 31 + day elif month == 5: answer = 31 + 28 + 31 + 30 + day elif month == 6: answer = 31 + 28 + 31 + 30 + 31 + day elif month == 7: answer = 31 + 28 + 31 + 30 + 31 + 30 + day elif month == 8: answer = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day elif month == 9: answer = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day elif month == 10: answer = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day elif month == 11: answer = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day elif month == 12: answer = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day if year % 4 == 0 and month > 2 and year != 1900: answer = answer + 1 return answer
그레고리력을 기준으로 하라고 되어있는데, 찾아보니 그레고리력은 1900년이라는 예외가 있다
년도를 4로 나누어서 나머지 없이 나누어 떨어지면 윤년이지만 1900년은 윤년이 아니다.....
'Problem Solving' 카테고리의 다른 글
74. Valid Palindrome (0) 2023.01.15 73. Fizz Buzz (0) 2023.01.14 71. Implement Queue using Stacks (0) 2023.01.14 70. 카드2 (0) 2023.01.14 69. 큐 2 (0) 2023.01.14