列表

详情


1154. 一年中的第几天

给你一个字符串 date ,按 YYYY-MM-DD 格式表示一个 现行公元纪年法 日期。返回该日期是当年的第几天。

 

示例 1:

输入:date = "2019-01-09"
输出:9
解释:给定日期是2019年的第九天。

示例 2:

输入:date = "2019-02-10"
输出:41

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int dayOfYear(string date) { } };

python3 解法, 执行用时: 112 ms, 内存消耗: 17.1 MB, 提交时间: 2023-12-31 09:56:33

class Solution:
    def dayOfYear(self, date: str) -> int:
        return datetime.datetime.strptime(date, "%Y-%m-%d").timetuple().tm_yday

java 解法, 执行用时: 26 ms, 内存消耗: 45 MB, 提交时间: 2023-12-31 09:55:57

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

class Solution {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public int dayOfYear(String date) {
        return LocalDate.parse(date, formatter).getDayOfYear();
    }
}

cpp 解法, 执行用时: 24 ms, 内存消耗: 6.2 MB, 提交时间: 2023-12-31 09:55:31

class Solution {
public:
    int dayOfYear(string date) {
        tm dt;
        istringstream(date) >> get_time(&dt, "%Y-%m-%d");
        return dt.tm_yday + 1;
    }
};

javascript 解法, 执行用时: 164 ms, 内存消耗: 57 MB, 提交时间: 2023-12-31 09:55:08

/**
 * @param {string} date
 * @return {number}
 */
var dayOfYear = function(date) {
    const d = new Date(date);
    return (d - new Date(d.getFullYear(), 0, 0)) / 86400000;
};

rust 解法, 执行用时: 4 ms, 内存消耗: 2 MB, 提交时间: 2023-09-15 14:57:59

impl Solution {
    pub fn day_of_year(date: String) -> i32 {
        const DAYS: &[i32] = &[0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
        let date = date.split('-').map(|x| x.parse::<i32>().unwrap()).collect::<Vec<_>>();
        let leap = date[0] % 4 == 0 && date[0] % 100 != 0 || date[0] % 400 == 0;
        DAYS[date[1] as usize] + date[2] + if date[1] > 2 && leap { 1 } else { 0 }
    }

    pub fn day_of_year2(date: String) -> i32 {
        let ymd = date.split('-').map(|x| x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
        let mut days = ymd[2];
        for i in 1..ymd[1] {
            match i {
                1 | 3 | 5 | 7 | 8 | 10 | 12 => days += 31,
                2 => days += if ymd[0] % 400 == 0 || (ymd[0] % 4 == 0 && ymd[0] % 100 != 0) { 29 } else { 28 },
                _ => days += 30,
            }
        }
        days
    }
}

python3 解法, 执行用时: 192 ms, 内存消耗: 16.1 MB, 提交时间: 2023-09-15 14:56:01

from datetime import datetime
class Solution:
    def dayOfYear(self, date: str) -> int:
        return int(datetime.strptime(date, '%Y-%m-%d').strftime("%j"))

cpp 解法, 执行用时: 4 ms, 内存消耗: 6.2 MB, 提交时间: 2023-09-15 14:55:13

class Solution {
public:
    int dayOfYear(string date) {
        tm t;
        strptime(date.c_str(), "%Y-%m-%d", &t);
        return t.tm_yday + 1;
    }

    int dayOfYear2(string date) {
        tm t{};
        //strptime(date.c_str(), "%Y-%m-%d", &t);
        istringstream ss(date);
        ss >> get_time(&t, "%Y-%m-%d");
        mktime(&t);
        return t.tm_yday + 1;
    }
};

cpp 解法, 执行用时: 16 ms, 内存消耗: 6.2 MB, 提交时间: 2023-09-15 14:53:43

class Solution {
public:
    int dayOfYear(string date) {
        int year = stoi(date.substr(0, 4));
        int month = stoi(date.substr(5, 2));
        int day = stoi(date.substr(8, 2));

        int amount[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            ++amount[1];
        }

        int ans = 0;
        for (int i = 0; i < month - 1; ++i) {
            ans += amount[i];
        }
        return ans + day;
    }
};

java 解法, 执行用时: 9 ms, 内存消耗: 43.2 MB, 提交时间: 2023-09-15 14:53:02

class Solution {
    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));

        int[] amount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            ++amount[1];
        }

        int ans = 0;
        for (int i = 0; i < month - 1; ++i) {
            ans += amount[i];
        }
        return ans + day;
    }
}

javascript 解法, 执行用时: 148 ms, 内存消耗: 49.3 MB, 提交时间: 2023-09-15 14:52:47

/**
 * @param {string} date
 * @return {number}
 */
var dayOfYear = function(date) {
    const year = +date.slice(0, 4);
    const month = +date.slice(5, 7);
    const day = +date.slice(8);

    const amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
        ++amount[1];
    }

    let ans = 0;
    for (let i = 0; i < month - 1; ++i) {
        ans += amount[i];
    }
    return ans + day;
};

golang 解法, 执行用时: 16 ms, 内存消耗: 4.2 MB, 提交时间: 2023-09-15 14:52:33

func dayOfYear(date string) int {
    year, _ := strconv.Atoi(date[:4])
    month, _ := strconv.Atoi(date[5:7])
    day, _ := strconv.Atoi(date[8:])

    days := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
        days[1]++
    }

    ans := day
    for _, d := range days[:month-1] {
        ans += d
    }
    return ans
}

python3 解法, 执行用时: 60 ms, 内存消耗: 16.1 MB, 提交时间: 2023-09-15 14:52:11

class Solution:
    def dayOfYear(self, date: str) -> int:
        year, month, day = [int(x) for x in date.split("-")]

        amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
            amount[1] += 1

        ans = sum(amount[:month - 1])
        return ans + day

php 解法, 执行用时: 44 ms, 内存消耗: 18.8 MB, 提交时间: 2022-08-10 15:58:46

class Solution {

    /**
     * @param String $date
     * @return Integer
     */
    function dayOfYear($date) {
        $year = substr($date, 0, 4);
        return (strtotime($date) - strtotime("{$year}-01-01") ) / 86400 + 1;
    }
}

golang 解法, 执行用时: 24 ms, 内存消耗: 5.1 MB, 提交时间: 2021-06-25 14:51:31

func dayOfYear(date string) int {
    t, _ := time.Parse("2006-01-02", date)
    return t.YearDay()
}

上一题