{"id":5418,"date":"2019-08-11T12:32:33","date_gmt":"2019-08-11T19:32:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5418"},"modified":"2019-08-11T12:32:55","modified_gmt":"2019-08-11T19:32:55","slug":"leetcode-1154-day-of-the-year","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1154-day-of-the-year\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1154. Day of the Year"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>date<\/code>&nbsp;representing a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Gregorian_calendar\" target=\"_blank\" rel=\"noreferrer noopener\">Gregorian&nbsp;calendar<\/a>&nbsp;date formatted as&nbsp;<code>YYYY-MM-DD<\/code>, return the day number of the year.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date = \"2019-01-09\"\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong> Given date is the 9th day of the year in 2019.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date = \"2019-02-10\"\n<strong>Output:<\/strong> 41\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date = \"2003-03-01\"\n<strong>Output:<\/strong> 60\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> date = \"2004-03-01\"\n<strong>Output:<\/strong> 61\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>date.length == 10<\/code><\/li><li><code>date[4] == date[7] == '-'<\/code>, and all other&nbsp;<code>date[i]<\/code>&#8216;s are digits<\/li><li><code>date<\/code>&nbsp;represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>Key: checking whether that year is a leap year or not.<br>is_leap = (year % 4 == 0 and year % 100 !=0) or year % 400 == 0<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def dayOfYear(self, date: str) -> int:\n    y, m, d = int(date.split(\"-\")[0]), int(date.split(\"-\")[1]), int(date.split(\"-\")[2])\n    leap = (y % 4 == 0 and y % 100 != 0) or y % 400 == 0\n    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n    s = sum(days[0:m-1]) + d\n    if leap and m > 2: s += 1\n    return s\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;date&nbsp;representing a&nbsp;Gregorian&nbsp;calendar&nbsp;date formatted as&nbsp;YYYY-MM-DD, return the day number of the year. Example 1: Input: date = &#8220;2019-01-09&#8221; Output: 9 Explanation: Given date is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,488,4],"class_list":["post-5418","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-leap-year","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5418"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5418\/revisions"}],"predecessor-version":[{"id":5420,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5418\/revisions\/5420"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}