LeetCode.1185- one week in the week (Day of the Week)

This is the first Ogawa 415 update, the first 448 Pian original

Look problems and prepare

Introduced today is LeetCode algorithm in title Easy level of 266 questions (overall title number is 1185 ). Given the date, day of the week the return date. Enter three integer representing the day, month and year.

Return to one of the following answers: {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

E.g:

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

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

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

Constraints :

  • The given date is the effective date of between 1971 and 2100.

The first solution

Subject requires us to calculate a given day of the week, directly call the Calendar class API, if the interview is not recommended.

public String dayOfTheWeek(int day, int month, int year) {
    String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"};
    Calendar cal = Calendar.getInstance();
    cal.set(year, month-1, day);
    int i = cal.get(Calendar.DAY_OF_WEEK)-1;
    if (i<0) {
        i = 0;
    }
    return week[i];
}


The second solution

The first step, first calculate the total number of days in 1971 from the year before the current year, average year plus 365 days, leap year plus 366 days.

The second step, and then to calculate the month of previous months and the number of days, leap year February has 29 days.

The third step, plus a number of days minus one day, because the sample is subject to the first day Sunday.

Step four days, will finally get together with the five days since January 1, 1971 was a Friday, then take more than seven.

public String dayOfTheWeek2(int day, int month, int year) {
    String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"};
    int[] dayOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    int total = 0;
    for (int i=1971; i<year; i++) {
        if (i%4 == 0) {
            total++;
        }
        total += 365;
    }
    if (year%4 == 0) {
        dayOfMonth[1] = 29;
    }
    for (int j=0; j<month-1; j++) {
        total += dayOfMonth[j];
    }
    total += day - 1;
    return week[(total + 5)%7];
}


A third solution

Use Zeller's formula to solve.

w =  (c/4- 2*c + y + y/4 + 26*(m + 1)/10 + d - 1) % 7;
  • c: represents the century, is the first two years, such as 1971, c it is 19.

  • y: after two years, such as 1971, y is 71.

  • m: is the month, 3 <= m <= 14, a year in January and February should be seen as 13 the previous year, 14 months to calculate, for example, 2003 January 1, 2002 to be considered as 13 months the 1st calculated.

  • d: is the day.

  • w: results, on behalf of the week.

One thing to note, the use of Zeller's formula calculated the number, there may be negative, and therefore need to add 7, and then take more than once in order to go as a string array index value.

public String dayOfTheWeek3(int day, int month, int year) {
    String[] week = { "Sunday", "Monday", "Tuesday", 
            "Wednesday", "Thursday", "Friday", "Saturday" };
    //如果月份小于3月,则月份加12,年份减1
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int J = year / 100; // 世纪,年份前两位
    int K = year % 100; // 年份,年份后两位
    int h = (K + K / 4 + J / 4 - 2 * J +
             26 * (month + 1) / 10 + day - 1) % 7;
    // h算出来可能为负数,需要补7后再取一次余
    return week[(h + 7) % 7];
}


A fourth solution

Using Kim Larsen formula to calculate.

Week = (Day + 2*Month + 3*(Month+1)/5 + Year + Year/4 - Year/100 + Year/400) % 7;

If the month is less than in March, the month plus 12, minus one year. For the week, the Kim Larsen formula where Monday is ranked first, came in last Sunday.

public String dayOfTheWeek4(int day, int month, int year) {
    String[] week = {"Monday", "Tuesday", "Wednesday", 
            "Thursday", "Friday", "Saturday", "Sunday"};
    if (month < 3) {
        month += 12;
        year -= 1;
    }
    int w = (day + 2*month + 3*(month+1)/5 + 
            year + year/4 - year/100 + year/400) % 7;
    return week[w];
}


summary

Thematic algorithm has now updated LeetCode algorithm of feature articles 272 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures either] in a keyword, to obtain a series of articles collection.

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, leave a message, it is the greatest reward in watching and supporting me!

Guess you like

Origin www.cnblogs.com/xiaochuan94/p/11588720.html