easy
https://leetcode.com/problems/excel-sheet-column-title/
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
思路: 只insert 值<=26时的单字母,所以用 n %26, 然后 n/=26。 下次依旧插在开头
public class Solution { public String convertToTitle(int n) { StringBuilder res = new StringBuilder(); //appending digits <=26 (use n%26) while(n>0){ n--; res.insert(0,(char)('A' + n%26)); n /= 26; } return res.toString(); } }
No comments:
Post a Comment