1. Description
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
2. Runtime Distribution
3. Submission Details
4. Example
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
5. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active"]
public String convertToTitle(int n) { StringBuilder sb = new StringBuilder(); while (n > 0) { sb.insert(0, (char) ((n - 1) % 26 + 'A')); n = (n - 1) / 26; } return sb.toString(); }
[/restab]
[/restabs]
6.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active" ]
public class LeetCode0168 { public String convertToTitle(int n) { StringBuilder sb = new StringBuilder(); while (n > 0) { sb.insert(0, (char) ((n - 1) % 26 + 'A')); n = (n - 1) / 26; } return sb.toString(); } public static void main(String[] args) { LeetCode0168 leetcode = new LeetCode0168(); System.out.println(leetcode.convertToTitle(2147483647)); } }
[/restab]
[/restabs]
Comments | NOTHING