LeetCode 168. Excel Sheet Column Title
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 168. Excel Sheet Column Title
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
LeetCode 168. Excel Sheet Column Title
分析
難度 易
來源
https://leetcode.com/problems/excel-sheet-column-title/
十進制轉26進制
注意,z表示26,而非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 ????...Example 1:
Input: 1 Output: "A"Example 2:
Input: 28 Output: "AB"Example 3:
Input: 701 Output: "ZY"解答
1 package LeetCode; 2 3 public class L168_ExcelSheetColumnTitle { 4 public String convertToTitle(int n) { 5 StringBuilder sb=new StringBuilder(); 6 while(n>0){ 7 if(n%26!=0){ 8 sb.append((char)(n%26+64)); 9 n/=26; 10 } 11 else{ 12 sb.append('Z'); 13 n=n/26-1; 14 } 15 } 16 return sb.reverse().toString(); 17 } 18 public static void main(String[] args){ 19 L168_ExcelSheetColumnTitle l168=new L168_ExcelSheetColumnTitle(); 20 System.out.println(l168.convertToTitle(52)); 21 } 22 }?
?
posted on 2018-11-12 17:32 flowingfog 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/flowingfog/p/9947861.html
總結
以上是生活随笔為你收集整理的LeetCode 168. Excel Sheet Column Title的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytest 15 fixture之au
- 下一篇: c# 中属性与字段