leetcode 168 Excel表列名称 171 Excel 表列序号

cpp 复制代码
class Solution {
public:
    string convertToTitle(int columnNumber) {
        int sum = columnNumber;
        long cols = 0;
        long tmp = 1;
        while(columnNumber){
            tmp = columnNumber;
            columnNumber/=26;
            if(tmp%26==0){
                --columnNumber;
            }
            ++cols;
        }
        std::cout<<"cols: "<<cols<<std::endl;

        std::string res="";
        int a = 0;
        for(int i=1;i<=cols;++i){
            sum-=pow(26,i-1);
        }
        //类比十进制 -> 特殊的26进制
        
        for(int i = cols;i>1;--i){
            a = pow(26,i-1);
            res+=(sum/a+'A');
            sum=sum%a;
        }
        res+=('A'+sum);
        
        return res;
    }
};
cpp 复制代码
class Solution {
public:
    int titleToNumber(string columnTitle) {
        int res = 0;
        int a = 0;
        for(int i = columnTitle.size()-1;i>=1;--i){
            a=pow(26,i);
            res+=a;
        }

        for(int i = 0;i< columnTitle.size();++i){
            a = pow(26,columnTitle.size()-1-i);
            res += a*(columnTitle[i]-'A');
        }

        return res+1;
        
    }
};
相关推荐
aaaameliaaa8 分钟前
结构体 结构体
c语言·笔记·算法
有Li1 小时前
【AI答疑】MR数据预处理步骤
人工智能·笔记·算法·语言模型·医学生
盛世宏博北京1 小时前
多传感器数据融合算法:实现档案库房温湿度全域无死角监测
算法·档案温湿度
AgentMaster2 小时前
零售行业智能客服系统怎么选?3 大场景落地与 4 款平台对比实践
大数据·人工智能·算法
Nil2082 小时前
leetcode 394字符串解码
leetcode
远航计算机2 小时前
客服记录怎么变成 AI 会引用的内容?五步清洗法
大数据·人工智能·算法·aigc
码流子3 小时前
几万路监控怎么接:高速公路视频汇聚平台的架构设计与落地坑点
大数据·人工智能·物联网·算法·架构
万物智能3 小时前
PWM散热风扇设置—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
前端·后端·算法
北极有牛3 小时前
cuda算子--矩阵转置
人工智能·算法
程序猫.4 小时前
算法刷题笔记:模拟题从入门到实战(含 LeetCode 例题与习题)
java·数据结构·算法