Excel Sheet Column Title
问题:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
思路:
进制访问问题
我的代码:
public class Solution { public String convertToTitle(int n) { StringBuffer sb = new StringBuffer(); if(n <= 0) return sb.toString(); while(n != 0) { sb.append(getChar((n - 1)%26)); n = (n - 1)/26; } return sb.reverse().toString(); } public char getChar(int num) { return (char)(‘A‘ + num); } }
他人代码:
public class Solution { public String convertToTitle(int n) { StringBuilder result = new StringBuilder(); while(n>0) { result.append((char) ((n-1)%26 + (int)‘A‘)); n = (n-1)/26; } return result.reverse().toString(); } }
学习之处:
原文:http://www.cnblogs.com/sunshisonghit/p/4338156.html