标签:
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.
https://leetcode.com/problems/excel-sheet-column-title/
1 package leetcode; 2 3 import java.util.Stack; 4 5 public class Solution 6 { 7 8 public static String convertToTitle(int n) 9 { 10 Stack<Character> sk = new Stack<Character>(); 11 while(n/26!=0) 12 { 13 int m=n%26; 14 if(m==0) 15 m+=26; 16 sk.push((char)(‘A‘+m-1)); 17 if(n%26==0) 18 n=n/26-1; 19 else 20 n=n/26; 21 } 22 if(n<26&&n!=0) 23 sk.push((char)(‘A‘+n-1)); 24 String t=""; 25 while(!sk.isEmpty()) 26 t=t+sk.pop(); 27 return t; 28 } 29 public static void main(String args[]) 30 { 31 System.out.println(convertToTitle(26)); 32 } 33 }
标签:
原文地址:http://www.cnblogs.com/qq1029579233/p/4403393.html