码迷,mamicode.com
首页 > 其他好文 > 详细

Excel Sheet Column Title

时间:2015-04-08 19:37:19      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

Excel Sheet Column Title

标签:

原文地址:http://www.cnblogs.com/qq1029579233/p/4403393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!