{"id":5531,"date":"2019-09-07T21:18:17","date_gmt":"2019-09-08T04:18:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5531"},"modified":"2019-09-07T21:18:46","modified_gmt":"2019-09-08T04:18:46","slug":"leetcode-1185-day-of-the-week","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1185-day-of-the-week\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1185. Day of the Week"},"content":{"rendered":"\n<p>Given a date, return the corresponding day of the week for that date.<\/p>\n\n\n\n<p>The input is given as three integers representing the&nbsp;<code>day<\/code>,&nbsp;<code>month<\/code>&nbsp;and&nbsp;<code>year<\/code>&nbsp;respectively.<\/p>\n\n\n\n<p>Return the answer as one of the following values&nbsp;<code>{\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"}<\/code>.<\/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> day = 31, month = 8, year = 2019\n<strong>Output:<\/strong> \"Saturday\"\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> day = 18, month = 7, year = 1999\n<strong>Output:<\/strong> \"Sunday\"\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> day = 15, month = 8, year = 1993\n<strong>Output:<\/strong> \"Sunday\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The given dates are valid&nbsp;dates between the years&nbsp;<code>1971<\/code>&nbsp;and&nbsp;<code>2100<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Solution: LeapYear<\/strong><\/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\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\ninline bool leapYear(int y) {\n  return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);\n}\nclass Solution {\npublic:\n  string dayOfTheWeek(int day, int month, int year) {    \n    vector<string> names = {\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"};\n    vector<int> days = {31, 28 + leapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n    int sum = 0;\n    for (int i = 1970; i < year; ++i)\n      sum += 365 + leapYear(i);    \n    for (int i = 1; i < month; ++i)\n      sum += days[i - 1];    \n    sum += day;\n    return names[(sum + 3) % 7];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a date, return the corresponding day of the week for that date. The input is given as three integers representing the&nbsp;day,&nbsp;month&nbsp;and&nbsp;year&nbsp;respectively. Return the answer&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[495,488,31],"class_list":["post-5531","post","type-post","status-publish","format-standard","hentry","category-math","tag-date","tag-leap-year","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5531","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=5531"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5531\/revisions"}],"predecessor-version":[{"id":5533,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5531\/revisions\/5533"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}