Excel Sheet Column Number & Excel Sheet Column Title

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 
 1 class Solution {
 2 public:
 3     int titleToNumber(string s) {
 4         if (s.empty()) return 0;
 5
 6         int result = 0;
 7         for (int i = 0; i < s.size(); i++) {
 8             result = 26 * result + (s[i] - ‘A‘ + 1);
 9         }
10         return result;
11     }
12 };

2. 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 
 1 class Solution {
 2 public:
 3     string convertToTitle(int n) {
 4         string result;
 5         while (n) {
 6             n--;
 7             int temp = n % 26;
 8             result = (char)(‘A‘ + temp) + result;
 9             n /= 26;
10         }
11         return result;
12     }
13 };
时间: 2024-09-30 18:54:52

Excel Sheet Column Number & Excel Sheet Column Title的相关文章

[LeetCode]61. 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

171. Excel Sheet Column Number Excel表格的字母转成数字

[抄题]: 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 ... Example 1: Input: "A" Output: 1 Example 2: Input: "AB"

[leetcode] Excel Sheet Column Title &amp; 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 思路: 10进制转26进制 .先求低位再求高位,与10进制转2进制一样. 题解: class Solution

LeetCode[Math]----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 分析: 给一个正整数,返回对应的Excel中列的表示.这种表示是用字母A-Z来表示的,以26为进制. 类比十进

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&

【leetcode】Excel Sheet Column Title &amp; Excel Sheet Column Number (easy)

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 思路: 相当于10进制转26进制.与一般不一样的是10进制对应的是0 - 9.而这个26进制对应的是 A(1)- Z(26), 没有0. 我的代码: strin

171. 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 static publi

【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 的代码让我

2015.5.18——Excel Sheet Column Number

Excel Sheet Column Number 本题收获: 1.对于字符串中字母转为ASIIC码:string s ;res = s[i]-'A'; 这个res就是数字s[i]-'A'是对ASIIC的操作. 2.对于进制的转换:利用for循环 ,%,/,数组在完成. 3.2中的数组都是从左到右的,在此题中没有按照高地位 题目: elated to question Excel Sheet Column Title Given a column title as appear in an Ex