【力扣(LeetCode)】【C/C++】【1309.解码字母到整数映射】

学习时间:

        2023年1月29日


题目描述:


 题解分享:

// 作     者 : 繁 华 倾 夏
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>                                                 // 调用strlen函数
#include <stdlib.h>                                                 // 调用malloc函数

// 力扣(LeetCode):1309. 解码字母到整数映射

// s:传入的字符串
char* freqAlphabets(char* s) {
    int len = strlen(s);                                            // 统计字符串长度
    char* re = (char*)malloc(sizeof(char) * len);                   // 创建返回字符串并初始化空间
    int i = 0, j = 0;
    for (i = 0; i < len; i++) {                                     // 遍历字符串
        if (i + 2 < len && s[i + 2] == '#') {                       // 当有长度为3并且第三个位置为#时
            re[j++] = (s[i] - '0') * 10 + (s[i + 1] - '1') + 'a';   // 将字符串转变后返回re
            i += 2;                                                 // 自增2
        }
        else {
            re[j++] = s[i] - '1' + 'a';                             // 将字符串转变后返回re
        }
    }
    re[j] = '\0';                                                   // 字符串最后需要赋'\0'
    return re;                                                      // 返回
}


// 测试用例
// 输入 s = "10#11#12"
// 输出 "jkab"
int main() {
    char s[] = "10#11#12";
    char* re = s;
    re = freqAlphabets(s);
    puts(re);
    return 0;
}

【繁华倾夏】【每日力扣题解分享】【Day15】


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值