码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode:171 Excel Sheet Column Number-每日编程第十题

时间:2015-12-09 18:49:29      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Excel Sheet Column Number

Total Accepted: 54683 Total Submissions: 140674 Difficulty: Easy

 

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 
思路:
假设num是一个五位数ABCDE.
num=A*26^4+B*26^3+c*26^2+D*26+E*1;
radix作为基数,初始化为1,每次循环*26;
 1 class Solution {
 2 public:
 3     int titleToNumber(string s) {
 4         int len=s.length();
 5         if(len==0){
 6             return 0;
 7         }
 8         int num=0;
 9         int radix=1;
10         for(int i=len-1;i>=0;i--){
11             num+=radix*(s[i]-A+1);
12             radix*=26;
13         }
14         return num;
15     }
16 };

 

leetcode:171 Excel Sheet Column Number-每日编程第十题

标签:

原文地址:http://www.cnblogs.com/yishuimao/p/5033308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!