public class Solution { private string Convert(int k) { var s = ""; switch (k) { case 1: s = "A"; break; case 2: s = "B"; break; case 3: s = "C"; break; case 4: s = "D"; break; case 5: s = "E"; break; case 6: s = "F"; break; case 7: s = "G"; break; case 8: s = "H"; break; case 9: s = "I"; break; case 10: s = "J"; break; case 11: s = "K"; break; case 12: s = "L"; break; case 13: s = "M"; break; case 14: s = "N"; break; case 15: s = "O"; break; case 16: s = "P"; break; case 17: s = "Q"; break; case 18: s = "R"; break; case 19: s = "S"; break; case 20: s = "T"; break; case 21: s = "U"; break; case 22: s = "V"; break; case 23: s = "W"; break; case 24: s = "X"; break; case 25: s = "Y"; break; case 0: s = "Z"; break; } return s; } public string ConvertToTitle(int n) { var list = new List<int>(); while (n != 0) { var k = n % 26; list.Add(k); if (k == 0) { n = n / 26 - 1; } else { n = n / 26; } } list.Reverse(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { var s = Convert(list[i]); sb.Append(s); } return sb.ToString(); } }
https://leetcode.com/problems/excel-sheet-column-title/#/description
时间: 2024-10-12 13:50:45