Excel Sheet Column Title

LeetCode Q 168 - Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

Example 1: Input: 1 ; Output: "A"

Example 2: Input: 28 ; Output: "AB"

Example 3: Input: 701 ; Output: "ZY"

Solution

Algorithm
Bases Convert

Code:

public String convertToTitle(int n) {
  StringBuilder sb = new StringBuilder();
  
  while (n != 0) {
    n--;
    sb.append((char) (n % 26 + 'A'));
    n /= 26;
  }

  return sb.reverse().toString();
}

   Reprint policy


《Excel Sheet Column Title》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
 Previous
Excel Sheet Column Number Excel Sheet Column Number
LeetCode Q 171 - Excel Sheet Column NumberGiven a column title as appear in an Excel sheet, return its corresponding col
2019-04-09 Tong Shi
Next 
Factorial Trailing Zeroes Factorial Trailing Zeroes
LeetCode Q 172 - Factorial Trailing ZeroesGiven an integer n, return the number of trailing zeroes in n!. Example 1: Inp
2019-04-09 Tong Shi
  TOC