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

Excel Sheet Column Title

时间:2014-12-21 00:44:20      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

Excel Sheet Column Title

 

Given a non-zero 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 

参考:http://www.cnblogs.com/ganganloveu/p/4175848.html
这道题以为很easy,但还是参考了别人的才A出来了
java中能用StringBuffer还是用StringBuffer,感觉StringBuffer要比String要好用一点
 1 public class Solution {
 2    public String convertToTitle(int n) {
 3         StringBuffer result = new StringBuffer();
 4         while(n > 0){
 5             n--;
 6             int temp = n % 26 + 65;
 7             result.append((char)temp);
 8             n = n / 26;
 9             
10         }
11         for(int i = 0, j = result.length() - 1; i < j;i++, j--){
12             char temp = result.charAt(i);
13             result.setCharAt(i, result.charAt(j));
14             result.setCharAt(j, temp);
15             
16         }
17         return result.toString();
18     }
19 }

 

Excel Sheet Column Title

标签:

原文地址:http://www.cnblogs.com/luckygxf/p/4176067.html

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