您的位置:首页 > 其它

LeetCode 171

2016-05-10 19:09 357 查看
Excel Sheet Column Number

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet,
return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

/*************************************************************************
> File Name: LeetCode171.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: 2016年05月10日 星期二 04时02分20秒
************************************************************************/

/*************************************************************************

Excel Sheet Column Number

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet,
return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

************************************************************************/

#include "stdio.h"

int titleToNumber(char* s)
{
int sum = 0;
int i;
for( i=0; i<strlen(s); i++ )
{
sum = 26*sum + (s[i]-'A'+1);
}
printf("%d\n",sum);
return sum;
}

int main()
{
char* s = "AB";
titleToNumber(s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: