LeetCode Excel Sheet Column Number

class Solution {
public:
    int titleToNumber(string s) {
        int len = s.length();
        if(len == 0) {
            return 0;
        }
        return s[len - 1] - 'A' + 1 + 26 * titleToNumber(s.substr(0, len - 1));
    }
};

水一发,非递归的感觉时间怎么更慢

class Solution {
public:
    int titleToNumber(string s) {
        int ret = 0;
        
        for (int i=0, len = s.length(); i < len; i++) {
            ret = ret * 26 + s[i] - 'A' + 1;
        }
        return ret;
    }
};

 

posted @ 2015-01-27 23:59  卖程序的小歪  阅读(118)  评论(0编辑  收藏  举报