Leetcode: Excel Sheet Column Number, leetcodesheet

Source: Internet
Author: User

Leetcode: Excel Sheet Column Number, leetcodesheet

I. Question

Returns the column number corresponding to a column title that appears in an Excel table.

For example:

A-> 1

B-> 2

C-> 3

...

Z-> 26

AA-> 27

AB-> 28

Ii. Analysis

The question is very clear. In fact, a careful analysis is very simple, that is, converting a string into a deformation of the integer (atoi ()-base on 26, and simpler.

 

class Solution {public:    int titleToNumber(string s) {        int sum = 0;        for(int i = 0; s[i] != '\0'; i++){            sum = sum *26 + (s[i] - 'A' + 1);        }        return sum;    }};

I just learned Python, but it's easy to see:

class Solution:    # @param s, a string    # @return an integer    def titleToNumber(self, s):        sum = 0        for i in s:            sum = sum * 26 + ord(i) -ord('A') + 1        return sum        


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.