0ms AC Java Solution


  • 0
    C
    public class Solution {
        public String convertToTitle(int n) {
            StringBuilder sb = new StringBuilder();
            while(n--!=0){
                sb.append((char)(n%26+'A'));
                n/=26;
            }
            return sb.length()==0?"A":sb.reverse().toString();
        }
    }

  • 1
    K

    When the input n = 0, the expected value shall be "", not "A".
    So the return clause need to be changed to:
    return sb.length()==0?"":sb.reverse().toString();

    the 0 input is actually not valid but I think it's better to return "" instead of "A".


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.