返回介绍

solution / 1100-1199 / 1185.Day of the Week / README_EN

发布于 2024-06-17 01:03:22 字数 5306 浏览 0 评论 0 收藏 0

1185. Day of the Week

中文文档

Description

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

 

Example 1:

Input: day = 31, month = 8, year = 2019
Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999
Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993
Output: "Sunday"

 

Constraints:

  • The given dates are valid dates between the years 1971 and 2100.

Solutions

Solution 1: Zeller's Congruence

We can use Zeller's Congruence to calculate the day of the week. Zeller's Congruence is as follows:

$$ w = (\left \lfloor \frac{c}{4} \right \rfloor - 2c + y + \left \lfloor \frac{y}{4} \right \rfloor + \left \lfloor \frac{13(m+1)}{5} \right \rfloor + d - 1) \bmod 7 $$

Where:

  • w: Day of the week (starting from Sunday)
  • c: First two digits of the year
  • y: Last two digits of the year
  • m: Month (the range of m is from 3 to 14, that is, in Zeller's Congruence, January and February of a certain year are considered as the 13th and 14th month of the previous year. For example, January 1, 2003 is considered as the 1st day of the 13th month of 2002)
  • d: Day
  • ⌊⌋: Floor function (round down)
  • mod: Modulo operation

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
    return datetime.date(year, month, day).strftime('%A')
import java.util.Calendar;

class Solution {
  private static final String[] WEEK
    = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

  public static String dayOfTheWeek(int day, int month, int year) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, day);
    return WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1];
  }
}
class Solution {
public:
  string dayOfTheWeek(int d, int m, int y) {
    if (m < 3) {
      m += 12;
      y -= 1;
    }
    int c = y / 100;
    y %= 100;
    int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7;
    vector<string> weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    return weeks[(w + 7) % 7];
  }
};
func dayOfTheWeek(d int, m int, y int) string {
  if m < 3 {
    m += 12
    y -= 1
  }
  c := y / 100
  y %= 100
  w := (c/4 - 2*c + y + y/4 + 13*(m+1)/5 + d - 1) % 7
  weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
  return weeks[(w+7)%7]
}
function dayOfTheWeek(d: number, m: number, y: number): string {
  if (m < 3) {
    m += 12;
    y -= 1;
  }
  const c: number = (y / 100) | 0;
  y %= 100;
  const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
  const weeks: string[] = [
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
  ];
  return weeks[(w + 7) % 7];
}

Solution 2

class Solution:
  def dayOfTheWeek(self, d: int, m: int, y: int) -> str:
    if m < 3:
      m += 12
      y -= 1
    c = y // 100
    y = y % 100
    w = (c // 4 - 2 * c + y + y // 4 + 13 * (m + 1) // 5 + d - 1) % 7
    return [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
    ][w]
class Solution {
  public String dayOfTheWeek(int d, int m, int y) {
    if (m < 3) {
      m += 12;
      y -= 1;
    }
    int c = y / 100;
    y %= 100;
    int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7;
    return new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
      "Saturday"}[(w + 7) % 7];
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文