leetcode:171 Excel Sheet Column Number-每日编程第十题

Excel Sheet Column Number

Total Accepted: 54683 Total Submissions: 140674 Difficulty: Easy

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 思路:假设num是一个五位数ABCDE.num=A*26^4+B*26^3+c*26^2+D*26+E*1;radix作为基数,初始化为1,每次循环*26;
 1 class Solution {
 2 public:
 3     int titleToNumber(string s) {
 4         int len=s.length();
 5         if(len==0){
 6             return 0;
 7         }
 8         int num=0;
 9         int radix=1;
10         for(int i=len-1;i>=0;i--){
11             num+=radix*(s[i]-‘A‘+1);
12             radix*=26;
13         }
14         return num;
15     }
16 };
时间: 2024-10-12 08:16:10

leetcode:171 Excel Sheet Column Number-每日编程第十题的相关文章

leetcode——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 Hide Tags: M

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

LeetCode: 171 Excel Sheet Column Number(easy)

题目: 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

Leetcode 171 Excel Sheet Column Number 难度:0

https://leetcode.com/problems/excel-sheet-column-number/ class Solution { public: int titleToNumber(string s) { int ans = 0; for(int i = 0;i < s.length();i++) { ans *= 26; ans += s[i] - 'A' + 1; } return ans; } };

Leetcode 171 Excel Sheet Column Number 字符串处理

本质是把26进制转化为10进制 A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int ans = 0; 5 for(int i = 0; i<s.size(); ++i){ 6 ans = 26 * ans + s[i] - 'A' + 1; 7 } 8 return ans; 9 }

LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p, q) { if(p

171. Excel Sheet Column Number(C++)

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 题目大意: 相

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】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