zoukankan      html  css  js  c++  java
  • Excel Sheet Column Title&&Excel Sheet Column Number

    Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet.

    For example:

        1 -> A
        2 -> B
        3 -> C
        ...
        26 -> Z
        27 -> AA
        28 -> AB 


    C++版:
    class Solution {
    public:
        string convertToTitle(int n) {
            string ret = "";
            while(n)
            {
                ret = (char)((n-1)%26+'A') + ret;
                n = (n-1)/26;
            }
            return ret;
        }
    };

    Python版:

    1,10进制转成26进制,但需要注意先减1。

    2,chr(i):返回整数i对应的ASCII字符。与ord()作用相反。

    3,逆序返回可以直接用res[::-1]。

    4,python中string类型的数据没有append(var), insert(index,var),pop(var)等list所拥有的方法,需要特别的注意.

    5,python不用像c++那样声明数据类型,但也可以声明,如string=''(字符串),list=[](列表),tuple=(1,1,2,3,)(元组声明后不能更改)

    class Solution:
        # @param {integer} n
        # @return {string}
        def convertToTitle(self, n):
            flag=n
            string=''
            while n!=0:
                rem=(n-1)%26
                n=(n-1)/26
                rem=chr(rem+65)
                string+=(rem)
            return string[::-1]

     Excel Sheet Column Number

    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 

    26进制转10进制,因为'A'以1开头而不是0,因此要“+1”

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



  • 相关阅读:
    eclipse 中配置查看jdk基础源代码
    网页授权获取用户基本信息
    语系/地区码
    windows 下 PHP DOITPHP框架 memcache缓存开启。
    xheditor富文本框 存值与展示问题
    逻辑处理
    ajax图片单个上传(ajaxfileupload.js)
    18 南京 D
    poj 2069
    poj 2420
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4568309.html
Copyright © 2011-2022 走看看