首页 > 其他 > 详细

Excel Sheet Column Title

时间:2015-04-08 19:36:46      阅读:89      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!