zoukankan      html  css  js  c++  java
  • LeetCode 6:Excel Sheet Column Number

    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 
    以上是问题,实际上很好做。但是。。。。
    以下是我的代码:
    class Solution
    {
    public:
        int titleToNumber(string s)
        {
            int strNum = 0;
            int strIdx = 1;
            int strLen = s.size();
            for (string::iterator iter=s.begin(); iter!=s.end(); ++iter)
            {
                strNum += ( *iter - 'A' + 1) * pow(static_cast<double>(26),static_cast<double>(strLen - strIdx));
                ++strIdx;
            }
            return strNum;
        }
    };

    定义了这么多变量,然后求出了一个strNum,看来问题是解决了。
    在网上搜的大神的代码:

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

    简单,就是这么任性,这就是个26进制转10十进制的问题!!!自己还是灰常的不足。。。
    多看看大神的代码,多学习,多吸收!!!

  • 相关阅读:
    Python面向对象高级编程
    Python面向对象编程
    Anaconda的安装
    Python模块
    Python函数式编程
    Python高级特性
    hdu 3065 病毒侵袭持续中 ac自动机
    hdu 2896 病毒侵袭 ac自动机
    poj 3667 Hotel 线段树
    hdu 4322 Candy 费用流
  • 原文地址:https://www.cnblogs.com/bestwangjie/p/4191664.html
Copyright © 2011-2022 走看看