The first few days (Day of the Year) in LeetCode.1154- year

This is the first Ogawa 410 update, the first 442 Pian original

Look problems and prepare

Introduced today is LeetCode algorithm in title Easy level of 261 questions (overall title number is 1154 ). Given representation format YYYY-MM-DDstring date of the Gregorian calendar, the date numbers in the return of the year. E.g:

Input: date = "2019-01-09"
Output: 9
Description: 2019 date is given in the 9th day.

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

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

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

Note :

  • date.length = 10

  • date [4] == date [7 ] == '-', all others date[i]are numeric

  • Dates represent calendar dates between January 1, 1900 to December 31, 2019.

The first solution

Title mean number of days in the year to date is calculated in, YEARS average year, a leap year, the month points Otsuki, Satsuki, directly on the date string interception, converted to the corresponding date figures. Year for treatment, if it is a leap year, February has 29 days, for a month of treatment, to determine the month, then the corresponding number of days to accumulate.

public int dayOfYear(String date) {
    int year = Integer.parseInt(date.substring(0, 4));
    int month = Integer.parseInt(date.substring(5, 7));
    int day = Integer.parseInt(date.substring(8, 10));
    switch (month) {
        case 12:
            day += 30;
        case 11:
            day += 31;
        case 10:
            day += 30;
        case 9:
            day += 31;
        case 8:
            day += 31;
        case 7:
            day += 30;
        case 6:
            day += 31;
        case 5:
            day += 30;
        case 4:
            day += 31;
        case 3:
            day += 28;
            if (isLeapYear(year)) {
                day++;
            }
        case 2:
            day += 31;
    }
    return day;
}

/**
 * 判断当前年份是否为闰年
 * @param year
 * @return
 */
public boolean isLeapYear(int year) {
    // 普通闰年,能被4整除,但是后两位不以00结尾
    if (year%4 == 0 && year%100 != 0) {
        return true;
    }
    // 世纪闰年,后两位以00结尾,且能被400整除
    if (year%100 == 0 && year%400 == 0) {
        return true;
    }
    return false;
}


The second solution

For the first solution in the switchstatement, we can replace an array, you can reduce the amount of code. In months for the index, the number of days value, use a loop to handle the number of days, as the judgment leap year is unchanged.

public int dayOfYear2(String date) {
    int[] arr = {31,28,31,30,31,30,31,31,30,31,30,31};
    int year = Integer.parseInt(date.substring(0, 4));
    int month = Integer.parseInt(date.substring(5, 7));
    int day = Integer.parseInt(date.substring(8, 10));
    for (int i=0; i<month-1; i++) {
        day += arr[i];
    }
    return isLeapYear(year) && month >= 3 ? day+1 : day;
}

/**
 * 判断当前年份是否为闰年
 * @param year
 * @return
 */
public boolean isLeapYear(int year) {
    // 普通闰年,能被4整除,但是后两位不以00结尾
    if (year%4 == 0 && year%100 != 0) {
        return true;
    }
    // 世纪闰年,后两位以00结尾,且能被400整除
    if (year%100 == 0 && year%400 == 0) {
        return true;
    }
    return false;
}


A third solution

Since we already know how many days per month, then we can turn directly to the cumulative number of days into the array, the array index was minus 1 month.

Create an array of length 13, the first term is zero, because in January the number of days you can get directly to the display, the second 31 days, the number of days in February because of the need to add 31 days of January, followed by back calculation , the cumulative number of days in February this array to average year basis. If the current year is a leap year, the month and more than 2 months, you will need to pay more on results day.

public int dayOfYear3(String date) {
    int[] arr = {0,31,59,90,120,151,181,212,243,273,304,334,365};
    int year = Integer.parseInt(date.substring(0, 4));
    int month = Integer.parseInt(date.substring(5, 7));
    int day = Integer.parseInt(date.substring(8, 10));
    if (isLeapYear(year) && month >= 3) {
        return arr[month-1]+day+1;
    }
    return arr[month-1]+day;
}

/**
 * 判断当前年份是否为闰年
 * @param year
 * @return
 */
public boolean isLeapYear(int year) {
    // 普通闰年,能被4整除,但是后两位不以00结尾
    if (year%4 == 0 && year%100 != 0) {
        return true;
    }
    // 世纪闰年,后两位以00结尾,且能被400整除
    if (year%100 == 0 && year%400 == 0) {
        return true;
    }
    return false;
}


summary

Thematic algorithm has now updated LeetCode algorithm of feature articles 267 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures Any words] in obtaining 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, message forwarding and support is the greatest reward for me!

Guess you like

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