首页 > 其他 > 详细

leetcode 171

时间:2016-11-08 13:17:29      阅读:195      评论:0      收藏:0      [点我收藏+]

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 

从A到Z依次代表从1到26,按照26进制进位。
代码如下:
 1 class Solution {
 2 public:
 3     int titleToNumber(string s) {
 4         int n = s.length();
 5         int m = n;
 6         int result = 0;
 7         if(n == 0)
 8         {
 9             return 0;
10         }
11         result = (int)s[m-1];
12         result -= 64;
13         for(int i = 1; i < n+1; i++)
14         {
15             m--;
16             if(m == 0)
17             {
18                 return result;
19             }
20             int p = 1;
21             for(int j = 0; j < i; j++)
22             {
23                 p *= 26;
24             }
25             int re = (int)s[m-1];
26             re -= 64;
27             result = result + re * p;
28         }
29         return result;
30     }
31 };

 

 

leetcode 171

原文:http://www.cnblogs.com/shellfishsplace/p/6042439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!