관리 메뉴

솜씨좋은장씨

[leetCode] 1154. Day of the Year (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1154. Day of the Year (Python)

솜씨좋은장씨 2020. 7. 25. 01:11
728x90
반응형

Given a string date representing a 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: Given date is the 9th day of the year in 2019.

Example 2:

Input: date = "2019-02-10"
Output: 41

Example 3:

Input: date = "2003-03-01"
Output: 60

Example 4:

Input: date = "2004-03-01"
Output: 61

Constraints:

  • date.length == 10
  • date[4] == date[7] == '-', and all other date[i]'s are digits
  • date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.

Solution

class Solution:
    def dayOfYear(self, date: str) -> int:
        year, month, day = int(date.split('-')[0]), int(date.split('-')[1]), int(date.split('-')[2])
        
        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        
        answer = sum(days[:month-1]) + day
        
        if ((year % 4 == 0 and year % 100 != 0) or year % 400 == 0) and month > 2:
            answer = answer + 1
            
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments