LeetCode#171 Excel Sheet Column Number

来源:互联网 发布:java面向对象的特点 编辑:程序博客网 时间:2024/06/15 10:36

key: math
Runtime: 11 ms / beats 38.09%
No reference

class Solution {public:    int titleToNumber(string s) {        int col = 0;        for(char i : s)        {            col = col * 26 + (i - 'A' + 1);        }        return col;    }};

Update: no for range
Runtime: 8 ms / beats 43.62%
No reference

class Solution {public:    int titleToNumber(string s) {        int col = 0;        for(int i = 0; i < s.size(); i++)        {            col = col * 26 + (s[i] - 'A' + 1);        }        return col;    }};
0 0
原创粉丝点击