Programming Riddle: How might you translate an Excel column name to a number?

前端 未结 28 1372
[愿得一人]
[愿得一人] 2020-11-29 22:26

I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It\'s about translating Excel column letters to actu

28条回答
  •  情歌与酒
    2020-11-29 22:47

    Assuming column A = 1

    int GetColumnNumber(string columnName)
    {
      int sum = 0;
      int exponent = 0;
      for(int i = columnName.Length - 1; i>=0; i--)
      {
        sum += (columnName[i] - 'A' + 1) *  (GetPower(26, exponent));
        exponent++;
      }
      return sum;
    }
    
    int GetPower(int number, int exponent)
    {
      int power = 1;
      for(int i=0; i

提交回复
热议问题