Easy

题目描述

给你一个字符串 date,它的格式为 Day Month Year,其中:

  • Day 是集合 {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"} 中的一个元素。
  • Month 是集合 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} 中的一个元素。
  • Year 的范围是 [1900, 2100]

请你将字符串转变为 YYYY-MM-DD 的格式,其中:

  • YYYY 表示 4 位的年份。
  • MM 表示 2 位的月份。
  • DD 表示 2 位的日期。

示例 1:

输入:date = "20th Oct 2052"
输出:"2052-10-20"

示例 2:

输入:date = "6th Jun 1933"
输出:"1933-06-06"

示例 3:

输入:date = "26th May 1960"
输出:"1960-05-26"

提示:

  • 给定日期保证是有效的,所以不需要处理无效日期。
  • 分别处理日、月、年的转换。
  • 注意日期总是有两个字符的后缀,去掉最后两个字符就能得到数字。

解题思路

解题思路

这道题要求我们将特殊格式的日期字符串转换为标准的 YYYY-MM-DD 格式。

核心思路:

  1. 字符串分割:将输入字符串按空格分割成三部分:日、月、年
  2. 日期处理:去掉日期字符串的后缀(“st”、“nd”、“rd”、“th”),提取数字并补零到两位
  3. 月份映射:建立月份简称到数字的映射关系,将英文月份转换为两位数字
  4. 年份处理:年份直接使用,已经是四位数

具体步骤:

  • 分割字符串得到 [day, month, year]
  • 处理 day:去除最后 2-3 个字符的后缀,转为整数再格式化为两位字符串
  • 处理 month:使用字典/数组映射月份名称到对应数字
  • 处理 year:直接使用
  • 按 “YYYY-MM-DD” 格式拼接结果

这种方法时间复杂度低,代码简洁清晰,是处理此类字符串格式转换问题的标准做法。

代码实现

class Solution {
public:
    string reformatDate(string date) {
        vector<string> months = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        unordered_map<string, string> monthMap;
        for (int i = 1; i <= 12; i++) {
            monthMap[months[i]] = (i < 10 ? "0" : "") + to_string(i);
        }
        
        stringstream ss(date);
        string day, month, year;
        ss >> day >> month >> year;
        
        // Extract day number
        string dayNum = day.substr(0, day.length() - 2);
        if (dayNum.length() == 1) dayNum = "0" + dayNum;
        
        return year + "-" + monthMap[month] + "-" + dayNum;
    }
};
class Solution:
    def reformatDate(self, date: str) -> str:
        months = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
                  "May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
                  "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"}
        
        day, month, year = date.split()
        
        # Extract day number and format to 2 digits
        day_num = day[:-2]
        day_formatted = day_num.zfill(2)
        
        return f"{year}-{months[month]}-{day_formatted}"
public class Solution {
    public string ReformatDate(string date) {
        Dictionary<string, string> months = new Dictionary<string, string> {
            {"Jan", "01"}, {"Feb", "02"}, {"Mar", "03"}, {"Apr", "04"},
            {"May", "05"}, {"Jun", "06"}, {"Jul", "07"}, {"Aug", "08"},
            {"Sep", "09"}, {"Oct", "10"}, {"Nov", "11"}, {"Dec", "12"}
        };
        
        string[] parts = date.Split(' ');
        string day = parts[0];
        string month = parts[1];
        string year = parts[2];
        
        // Extract day number and format to 2 digits
        string dayNum = day.Substring(0, day.Length - 2);
        string dayFormatted = dayNum.PadLeft(2, '0');
        
        return $"{year}-{months[month]}-{dayFormatted}";
    }
}
var reformatDate = function(date) {
    const months = {
        "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
        "May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
        "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"
    };
    
    const [day, month, year] = date.split(' ');
    
    // Extract day number and format to 2 digits
    const dayNum = day.slice(0, -2);
    const dayFormatted = dayNum.padStart(2, '0');
    
    return `${year}-${months[month]}-${dayFormatted}`;
};

复杂度分析

复杂度大小
时间复杂度O(1)
空间复杂度O(1)

说明:

  • 时间复杂度:O(1),因为字符串长度固定,分割、查找、拼接操作都是常数时间
  • 空间复杂度:O(1),使用了固定大小的月份映射表,不随输入规模变化