leetCode(48):Excel Sheet Column Number And 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 
class Solution {
public:
    int titleToNumber(string s) {
        int result=0;
        for(int i=0;i<s.size();++i)
        {
            result=result*26+(s[i]-64);
        }
        return result;
    }
};

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 

class Solution {
public:
    string convertToTitle(int n) {
        string resultSrt;

    	while (n != 0)
    	{//string和容器比较类似
    		int tmp = (n-1) % 26;
		resultSrt.push_back('A'+tmp);//并不单纯的是一个26进制数
    		n = (n-1) / 26;
    	}

    	reverse(resultSrt.begin(), resultSrt.end());
    	return resultSrt;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 09:43:59

leetCode(48):Excel Sheet Column Number And Excel Sheet Column Title的相关文章

[LeetCode] Excel Sheet Column Number 求Excel表列序号

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 Credits:Special thanks to @ts for addi

[C++]LeetCode: 116 Excel Sheet Column Number (excel列坐标转数字)

题目: 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 思路:这道题和Excel Sheet Column Title和是对

【LeetCode】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 思路: 其实就是个进制转换.水水就过.倒是 Python 的代码让我

Excel Sheet Column Number -- leetcode

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 class Solution { public: int titleToNu

【LeetCode从零单刷】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 解答: 怎么说,类似26进制数一样.乍看之下挺简单.于是写下: class Solution { public: int titleToNumber(stri

[LeetCode] Excel Sheet Column Number

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 解题思路: 看成是进制转

【LeetCode】171 - 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 Solution:26进制转十进制,AAA=27*26+1 1 class

leetcode: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 这题题目的意思就是:将26进制字母转化为10进制数字 class Solution { public: int titleToNumber(string s) { i

LeetCode Excel Sheet Column Number 表列数

题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA"=26*2+1=53,"CBA"=26*26*3+26*2+1.相当于321(十进制),那么就是10*10*3+10*2+1,说第3条式子,C在第3位,在第1位时就是1~26的值了,也就是3,在第2位时就是将3*26了,再第3位时就是将3*26*26了:同理B就是2*26啦,A就是1