Excel Sheet Column Title - Math - Easy - LeetCode
💻 coding

Excel Sheet Column Title - Math - Easy - LeetCode

1 min read 121 words
1 min read
ShareWhatsAppPost on X
  • 1The article explains how to convert a positive integer to its corresponding Excel column title.
  • 2Examples include converting 1 to 'A', 28 to 'AB', and 701 to 'ZY'.
  • 3The provided solution uses a loop and string manipulation to achieve the conversion efficiently.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The article explains how to convert a positive integer to its corresponding Excel column title."

Excel Sheet Column Title - Math - Easy - LeetCode

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 ... Example 1:

Input: 1 Output: "A" Example 2:

Input: 28 Output: "AB" Example 3:

Input: 701 Output: "ZY"

public class Solution {
 public string ConvertToTitle(int n) {
 var sb = new StringBuilder(); 
 while(n>0){
 int mod = (n%26==0?26:n%26);
 char ch = (char) (64+mod); 
 sb.Append(ch.ToString()); 
 n = (n%26==0? n/26-1: n/26); 
 }
 
 var res = sb.ToString();
 return Reverse(res);
 }
 
 public static string Reverse( string s )
 {
 char[] charArray = s.ToCharArray();
 Array.Reverse( charArray );
 return new string( charArray );
 }
}

Time Complexity: O(n)

Space Complexity: O(1)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 1 October 2020 · 1 min read · 121 words

Part of AskGif Blog · coding

You might also like