【leetcode_easy_string】1507. Reformat Date

problem

1507. Reformat Date

solution#1: 使用map;使用函数;

code:

复制代码
class Solution {
public:
    unordered_map<string, string> 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"}};
    string reformatDate(string date) {   
        string res = "";
        if(date.size()==12)
        {
            // res = date.substr(8,4) + "-" + mon(date.substr(4, 3)) + "-0" + date.substr(0,1);
            res = date.substr(8,4) + "-" + months[date.substr(4, 3)] + "-0" + date.substr(0,1);

        }
        else
        {
           // res = date.substr(9,4) + "-" + mon(date.substr(5, 3)) + "-" + date.substr(0,2);
           res = date.substr(9,4) + "-" + months[date.substr(5, 3)] + "-" + date.substr(0,2);
        }
        return res;
    }
    string mon(string s)
    {
        string res = "";
        if(s=="Jan") res = "01";
        else if(s=="Feb") res = "02";
        else if(s=="Mar") res = "03";
        else if(s=="Apr") res = "04";
        else if(s=="May") res = "05";
        else if(s=="Jun") res = "06";
        else if(s=="Jul") res = "07";
        else if(s=="Aug") res = "08";
        else if(s=="Sep") res = "09";
        else if(s=="Oct") res = "10";
        else if(s=="Nov") res = "11";
        else if(s=="Dec") res = "12";
        return res;
    }
};
复制代码

 注意,通过字符串长度区分日期;

参考

1. leetcode_easy_string_1507. Reformat Date;

posted on   鹅要长大  阅读(95)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2019-02-25 【c++基础】c++提升速度的方法总结
2019-02-25 【leetcode】350. Intersection of Two Arrays II
2019-02-25 【leetcode】349. Intersection of Two Arrays
2019-02-25 【c++基础】static修饰的函数作用与意义
2019-02-25 【leetcode】345. Reverse Vowels of a String

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示