[leetcode-168-Excel Sheet Column Title]

Posted hellowOOOrld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-168-Excel Sheet Column Title]相关的知识,希望对你有一定的参考价值。

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

思路:

类似于进制转换,但是感觉自己写的太啰嗦了,待优化。

string convertToTitle(int n) 
 { 
   if(n<=0) return "";
   map<int,char>dict;
   dict[0] = Z;
   for(int i =1;i<=26;i++)
   {
     dict[i] =A + i -1;
  }
  string ret;
  stringstream ss;  
  while(n>0)
  {   
      ss<<dict[n%26];
      ret.insert(0, ss.str().c_str());
      if(n%26 ==0)
      {
    n/=26;
    n--;
      }
      else n/=26;
     ss.str("");
  }
  return ret;
}

 









以上是关于[leetcode-168-Excel Sheet Column Title]的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 168: Excel Sheet Column Title

LeetCode 168. Excel Sheet Column Title

LeetCode 168. Excel Sheet Column Title

[leetcode-168-Excel Sheet Column Title]

LeetCode168. Excel Sheet Column Title

Leetcode 168. Excel Sheet Column Title