52 ms C# solution


  • 0
    M
     public string ConvertToTitle(int n)
     {
          if (n <= 26)
                {
                    char single = (char)(n + 64);
                    string s = single.ToString();
                    return s;
                }
                string res = string.Empty;
                int rem = 0;
                int mod = n % 26;
                if (mod == 0)
                        rem = n / 26 - 1;
                    else
                        rem = n / 26;
    
                if (rem <= 26)
                {
                    int alphabet = n - (rem * 26);
                    char first = (char)(rem + 64);
                    char second = (char)(alphabet + 64);
                    res = first.ToString() + second.ToString();
                }
                else
                {
                    res = ConvertToTitle(rem) + ConvertToTitle(mod);
                }
    
                    return res;
        }

Log in to reply
 

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