My 10-line simple Java solution, without stringbuilders


  • 2
    H
    public class Solution {
        public String convertToTitle(int n) {
            String s = "";
            while(n != 0){
                s = (char)('A'+ (n - 1) % 26) + s;
                n = (n-1) / 26;
            }
            return s;
        }
    }

  • 0
    S

    Hey Hai4,

    I am sorry, but I don't get it, why is using a normal string better than stringBuilder? using StringBuilder must be of better performance , as it doesn't copy all the characters in a new string every cycle in the loop.

    Please Clarify if I am wrong.


  • 0
    H

    yes, you are right. I totally agree with your advice. Thanks for pointing it out.


Log in to reply
 

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