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;
        
    }
};
相关推荐
Dumbness1 小时前
UIA 读微信群消息没有 id,怎么判断哪条是新的:后缀对齐在翻历史和撤回时整屏重报
python·算法
taka1 小时前
从 24x 到 141x:我用纯 Python 零依赖项目,扒开了 GPU 加速的性能黑洞
算法
UIU1141 小时前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯
旖旎夜光1 小时前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
像风一样自由20202 小时前
2.如何用FastAPI把一个Python算法封装成HTTP服务
python·算法·fastapi
D e re k ZH2 小时前
保序回归:从 PAVA 到可并堆的单调约束优化
c++·算法·数据挖掘·回归·kotlin
青少儿编程课堂2 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:字典树前缀树与单词检索统计
c++·python·算法·bfs·信息学竞赛
文丑颜不良啊2 小时前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt2 小时前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表
文丑颜不良啊2 小时前
LeetCode1341~1360题目解析
java·leetcode