LeetCode解题----建立数据表


LeetCode解题中,特别是针对字符串类型的题目,有时有建立数据表的需要。这里基于Leetcode1507题简单介绍一下C语言中建立数据表的方式。

背景题目

1507转变日期格式
给你一个字符串 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"

提示:

给定日期保证是合法的,所以不需要处理异常输入。

基于结构体数据类型构造数据表

结构体定义

struct TABLE
{
    char *source; //输入字符
    char *des; //待匹配字符(也可以定义为int类型,依据题目要求)
};

数据表初始化

struct TABLE g_month[12] = {
    {"Jan", "01"},
    {"Feb", "02"},
    {"Mar", "03"},
    {"Apr", "04"},
    {"May", "05"},
    {"Jun", "06"},
    {"Jul", "07"},
    {"Aug", "08"},
    {"Sep", "09"},
    {"Oct", "10"},
    {"Nov", "11"},
    {"Dec", "12"}};

struct TABLE g_day[] = {
    {"1st", "01"},
    {"2nd", "02"},
    {"3rd", "03"},
    {"4th", "04"},
    {"5th", "05"},
    {"6th", "06"},
    {"7th", "07"},
    {"8th", "08"},
    {"9th", "09"},
    {"10th", "10"},
    {"11th", "11"},
    {"12th", "12"},
    {"13th", "13"},
    {"14th", "14"},
    {"15th", "15"},
    {"16th", "16"},
    {"17th", "17"},
    {"18th", "18"},
    {"19th", "19"},
    {"20th", "20"},
    {"21st", "21"},
    {"22nd", "22"},
    {"23rd", "23"},
    {"24th", "24"},
    {"25th", "25"},
    {"26th", "26"},
    {"27th", "27"},
    {"28th", "28"},
    {"29th", "29"},
    {"30th", "30"},
    {"31st", "31"},
};

函数主体

// #include 
// #include 
// #include 
struct TABLE
{
    char *source;
    char *des;
};
struct TABLE g_month[12] = {
    {"Jan", "01"},
    {"Feb", "02"},
    {"Mar", "03"},
    {"Apr", "04"},
    {"May", "05"},
    {"Jun", "06"},
    {"Jul", "07"},
    {"Aug", "08"},
    {"Sep", "09"},
    {"Oct", "10"},
    {"Nov", "11"},
    {"Dec", "12"}};

struct TABLE g_day[] = {
    {"1st", "01"},
    {"2nd", "02"},
    {"3rd", "03"},
    {"4th", "04"},
    {"5th", "05"},
    {"6th", "06"},
    {"7th", "07"},
    {"8th", "08"},
    {"9th", "09"},
    {"10th", "10"},
    {"11th", "11"},
    {"12th", "12"},
    {"13th", "13"},
    {"14th", "14"},
    {"15th", "15"},
    {"16th", "16"},
    {"17th", "17"},
    {"18th", "18"},
    {"19th", "19"},
    {"20th", "20"},
    {"21st", "21"},
    {"22nd", "22"},
    {"23rd", "23"},
    {"24th", "24"},
    {"25th", "25"},
    {"26th", "26"},
    {"27th", "27"},
    {"28th", "28"},
    {"29th", "29"},
    {"30th", "30"},
    {"31st", "31"},
};
char *reformatDate(char *date)
{
    char *day;
    char *month;
    char *year;
    char *result = (char *)malloc(11 * sizeof(char));
    memset(result, 0, sizeof(char) * 11);
    //函数原型:char *strtok(char *str, const char *delim)
    //参数说明:str -- 要被分解成一组小字符串的字符串;delim -- 包含分隔符的 C 字符串。
    day = strtok(date, " ");
    month = strtok(NULL, " ");
    year = strtok(NULL, " ");
    for (int i = 0; i < 12; i++)
    {
        if (strcmp(month, g_month[i].source) == 0) // 依据输入建立匹配
        {
            month = g_month[i].des;
            break;
        }
    }
    for (int i = 0; i < 31; i++)
    {
        if (strcmp(day, g_day[i].source) == 0) // 依据输入建立匹配
        {
            day = g_day[i].des;
            break;
        }
    }
    //printf("%s\n", result);
    strcat(result, year);
    strcat(result, "-");
    strcat(result, month);
    strcat(result, "-");
    strcat(result, day);
    return result;
}
// int main()
// {
//     char s[] = "20th Oct 2052";
//     char *date = (char *)malloc(14 * sizeof(char));
//     date = s;
//     char *result = (char *)malloc(11 * sizeof(char));
//     result = reformatDate(date);
//     printf("%s", result);
//     return 0;
// }