本文主要是介绍[LeetCode 168] Excel Sheet Column Title,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB
solution
10进制转为26进制
1.
public String convertToTitle(int n) {Map<Integer, String> numToString = new HashMap<>();for(int i=0;i<26;i++){char w = (char) ('A' + i);numToString.put(i, String.valueOf(w));}if(n<=26) return numToString.get(n-1);StringBuilder sb = new StringBuilder();while(n>0){int index = (n-1)%26;sb.append(numToString.get(index));n = (n-1)/26;}return sb.reverse().toString();}
2.
public String convertToTitle1(int n) {String result = "";while(n>0){result = (char)((n-1)%26 +'A') + result;n = (n-1)/26;}return result;}
这篇关于[LeetCode 168] Excel Sheet Column Title的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!