LeetCode 171

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

 

 1 /*************************************************************************
 2     > File Name: LeetCode171.c
 3     > Author: Juntaran
 4     > Mail: Jacinthmail@gmail.com
 5     > Created Time: 2016年05月10日 星期二 04时02分20秒
 6  ************************************************************************/
 7 
 8 /*************************************************************************
 9 
10     Excel Sheet Column Number
11 
12     Related to question Excel Sheet Column Title
13 
14     Given a column title as appear in an Excel sheet, 
15     return its corresponding column number.
16 
17     For example:
18 
19         A -> 1
20         B -> 2
21         C -> 3
22         ...
23         Z -> 26
24         AA -> 27
25         AB -> 28
26 
27  ************************************************************************/
28 
29 #include "stdio.h"
30 
31 int titleToNumber(char* s)
32 {
33     int sum = 0;
34     int i;
35     for( i=0; i<strlen(s); i++ )
36     {
37         sum = 26*sum + (s[i]-'A'+1);
38     }
39     printf("%d\n",sum);
40     return sum;
41 }
42 
43 int main()
44 {
45     char* s = "AB";
46     titleToNumber(s);
47     return 0;
48 }

 

posted @ 2016-05-10 19:09  Juntaran  阅读(158)  评论(0编辑  收藏  举报