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();
    }
}
    原文作者:billyzhang
    原文地址: https://www.jianshu.com/p/c8a844b1af62
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞