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;
        
    }
};
相关推荐
Xの哲學2 分钟前
深入剖析Linux文件系统数据结构实现机制
linux·运维·网络·数据结构·算法
AlenTech17 分钟前
200. 岛屿数量 - 力扣(LeetCode)
算法·leetcode·职场和发展
C雨后彩虹26 分钟前
竖直四子棋
java·数据结构·算法·华为·面试
不如自挂东南吱1 小时前
空间相关性 和 怎么捕捉空间相关性
人工智能·深度学习·算法·机器学习·时序数据库
Excel工作圈1 小时前
凭证助手一键匹配已勾选抵扣发票与全量发票明细
数据库·excel
洛生&1 小时前
Elevator Rides
算法
2501_933513042 小时前
关于一种计数的讨论、ARC212C Solution
算法
Wu_Dylan2 小时前
智能体系列(二):规划(Planning):从 CoT、ToT 到动态采样与搜索
人工智能·算法
散峰而望2 小时前
【算法竞赛】栈和 stack
开发语言·数据结构·c++·算法·leetcode·github·推荐算法
知乎的哥廷根数学学派2 小时前
基于多尺度注意力机制融合连续小波变换与原型网络的滚动轴承小样本故障诊断方法(Pytorch)
网络·人工智能·pytorch·python·深度学习·算法·机器学习