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十进制的问题!!!自己还是灰常的不足。。。
    多看看大神的代码,多学习,多吸收!!!

  • 相关阅读:
    [转载]四大Java EE容器
    [转载]javaEE规范和SSH三大框架到底有什么关系
    javaee包含的服务和组件
    Java类文件最大限制
    oracle给字段添加描述
    apache commons工具包
    redis教程
    git学习
    编程人物
    程序员必须了解的5大编程准则
  • 原文地址:https://www.cnblogs.com/bestwangjie/p/4191664.html
Copyright © 2011-2022 走看看