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;
        
    }
};
相关推荐
我也不曾来过140 分钟前
AVL树的实现
算法
jerry6091 小时前
LeetCode 滑动窗口问题 - 核心限制条件总结 (基于灵茶山艾府分类 - 详尽版)
算法·leetcode·分类
yuhao__z1 小时前
代码随想录算法训练营第六十五天| 图论10—卡码网94. 城市间货物运输 I,95. 城市间货物运输 II
java·算法·图论
袁气满满~_~1 小时前
LeetCode:贪心算法
算法·leetcode·贪心算法
枫景Maple1 小时前
LeetCode 1340. 跳跃游戏 V(困难)
算法·leetcode
虾球xz1 小时前
游戏引擎学习第304天:构建与遍历图
c++·学习·算法·游戏引擎
虾球xz1 小时前
游戏引擎学习第300天:从排序键更改为排序规则
c++·学习·算法·游戏引擎
新知图书1 小时前
OpenCV图像平移示例
opencv·算法·计算机视觉
FungLeo2 小时前
浏览器原生 Web Crypto API 实现 SHA256 Hash 加密
前端·算法·哈希算法·sha256·web crypto api
apcipot_rain2 小时前
【应用密码学】实验六 公钥密码3——SM2
算法·密码学·哈希算法