[Leetcode 171, Easy]Excel Sheet Column Number

来源:互联网 发布:linux vi创建文件 编辑:程序博客网 时间:2024/05/17 03:57

Problem:

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 

Analysis:

This problem is similar to the converting of numbers, i.e., from 10-based to 2-based.

Solutions:

C++:

int titleToNumber(string s) {        vector<int> digits(s.size() + 1, 0);        for(int i = s.size() - 1, digit_index = 0; i >= 0; --i)            if(s[i] != 'Z')                digits[digit_index++] += s[i] - 'A' + 1;            else {                digits[digit_index++] += 0;                digits[digit_index] += 1;            }                int rInteger = 0;        for(int i = 0; i < digits.size(); ++i)            rInteger += digits[i]*(int(pow(26, i)));        return rInteger;    }


Java:


Python:


0 0
原创粉丝点击