Blogs Hub

by Sumit Chourasia | Oct 01, 2020 | Category :coding | Tags : algorithmडेटा-संरचनाआसानleetcodeगणित

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)

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia