Skip to content

Excel Sheet Column Title

Tim_Gao edited this page Sep 14, 2016 · 1 revision
  1. Just like permutation sequence, use (n-1)%base
public class Solution {
    public String convertToTitle(int n) {
        StringBuilder res = new StringBuilder();
        while (n!=0){
            int crt = n%26;
            if (crt == 0){
                res.append("Z");
            } else {
                char ch = (char) ('A' + crt-1);
                res.append(ch);
            }
            n = (n-1)/26;
        }
        return res.reverse().toString();
    }
}

Clone this wiki locally