LeedCode OJ]#171 Excel Sheet Column Number

【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:[email protected]】

题目链接:https://leetcode.com/problems/excel-sheet-column-number/

题意:

给定A~Z,分别代表1~26,AA-27,AB-28等等,现在给定一个由字母组成的字符串,要求将其转换为数字

思路:

运用进制的思想,转换成求进制的思路即可

class Solution
{
public:
    int titleToNumber(string s)
    {
        int ans = 0;
        int len = s.length(),i,j;
        for(i = 0; i<len; i++)
        {
            int cnt = len-1-i;
            int a = s[i]-'A'+1,q = 1;
            while(cnt--)
                q*=26;
            ans=ans+a*q;
        }
        return ans;
    }
};

版权声明:本文为博主原创文章,如果转载,请注明出处

时间: 2024-10-18 18:51:55

LeedCode OJ]#171 Excel Sheet Column Number的相关文章

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

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

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 (表格字母与整数的映射)

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-每日编程第十题

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 -

[leedcode 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 public class Solution { public int tit

【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[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 OJ: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 {