Linear time complexity O(n). Java.

  • 2

    The time complexity is linear O(n) where n is the size of expected string.

    public String convertToTitle(int n) {
            int SIZE = 26;
            StringBuilder sb = new StringBuilder();
            int tail;
            while (n != 0) {
                tail = n % SIZE;
                n /= SIZE;
                sb.append((char) ('A' + tail));
            return sb.reverse().toString();

Log in to reply

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