LeetCode Excel Sheet Column Title

18 篇文章 0 订阅

原题链接在这里: https://leetcode.com/problems/excel-sheet-column-title/

刚开始以为是简单地26进制,结果有个特殊情况就是"Z", n=26, return "Z"; n = 52, return "AZ". 当current digit 是26时,要把当前位increase 26,并且总体的值要减去26,因为when n = 52, it should return "AZ" but not "BZ".

AC Java:

public class Solution {
    public String convertToTitle(int n) {
        if(n<=0)
            return "";
        
        StringBuilder sbStr = new StringBuilder();
        int lastDigit;
        char lastChar;
        while(n>0){
            lastDigit = n%26; //error
            if(lastDigit == 0){
                lastDigit += 26;
                n-=26;
            }
            lastChar = (char)(lastDigit+64);
            sbStr.insert(0,lastChar);
            n = n/26;
        }
        return sbStr.toString();
        
    }
}
Excel Sheet Column Number呼应。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值