168. Excel Sheet Column Title

168- Excel Sheet Column Title
My Submissions
Question
Editorial Solution
Total Accepted: 64257 Total Submissions: 288620 Difficulty: Easy

Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.

Hide Company Tags Microsoft Facebook Zenefits
Hide Tags Math
Hide Similar Problems (E) Excel Sheet Column Number

public class Solution {
    
    public static void main(String[] args) {
        Solution sl = new Solution();
        int[] nums = { -1, 0, 1, 26, 27, 28, 731};
        for (int n: nums) {
            System.out.print(sl.convertToTitle(n) + " \t ");
        }
        
    }
   
    
    public String convertToTitle(int n) {
        
        StringBuilder res = new StringBuilder();
        while (n > 0) {
            n--;
            res.insert(0, (char) ('A' + (n %26) ));
            n /= 26;
        }
        return res.toString();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容