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;
        
    }
};
相关推荐
Mr_Xuhhh20 小时前
算法题解博客:三道经典题目的思路与实现
算法
算法-大模型备案 多米20 小时前
大模型备案实操指南:材料、流程与避坑要点
大数据·网络·人工智能·算法·文心一言
顾温20 小时前
数据转换函数
开发语言·算法
汉克老师20 小时前
GESP2025年6月认证C++三级( 第三部分编程题(1、奇偶校验)
c++·算法·gesp三级·gesp3级·按位操作
Fcy64820 小时前
算法基础详解(一)模拟算法与高精度算法
算法·模拟算法·高精度算法
Promise微笑20 小时前
算法对齐还是实战突围?解构GEO优化中方法论与实践的权重博弈
算法
米粒121 小时前
力扣算法刷题 Day 29
算法·leetcode·职场和发展
wfbcg21 小时前
每日算法练习:LeetCode 125. 验证回文串 ✅
算法·leetcode·职场和发展
We་ct21 小时前
LeetCode 295. 数据流的中位数:双堆解法实战解析
开发语言·前端·数据结构·算法·leetcode·typescript·数据流
Aaron158821 小时前
RFSOC+VU13P/VU9P+GPU通用一体化硬件平台
人工智能·算法·fpga开发·硬件架构·硬件工程·信息与通信·基带工程