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
...
AAA -> 703
AAB -> 704
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
分析
用过Execl的都知道,上述字母是其列名称,数字代表第几列,本题是求给定一个A~Z组成的字符串序列,求出其在Execl表中的列数。
由以上给出的示例可以看出规律,其中A~Z代表数字1~26,那么字符串怎么求呢?
比如(由低到高逐位计算):
A=1∗260=1
A=2∗260=2
...
AA=1∗260+1∗261=27
AB=2∗260+1∗261=28
...
AAA=1∗260+1∗261+1∗262=703
AAB=2∗260+1∗261+1∗262=704
AC代码
class Solution {
public:
int titleToNumber(string s) {
int len = strlen(s.c_str());
if (len == 0)
return 0;
int count = 0 , p = 0;
for (int i = len-1; i >= 0; i--)
{
count = count + Num(s[i]) * pow(26, p);
p++;
}
return count;
}
int Num(char c)
{
switch (c)
{
case 'A':
return 1; break;
case 'B':
return 2; break;
case 'C':
return 3; break;
case 'D':
return 4; break;
case 'E':
return 5; break;
case 'F':
return 6; break;
case 'G':
return 7; break;
case 'H':
return 8; break;
case 'I':
return 9; break;
case 'J':
return 10; break;
case 'K':
return 11; break;
case 'L':
return 12; break;
case 'M':
return 13; break;
case 'N':
return 14; break;
case 'O':
return 15; break;
case 'P':
return 16; break;
case 'Q':
return 17; break;
case 'R':
return 18; break;
case 'S':
return 19; break;
case 'T':
return 20; break;
case 'U':
return 21; break;
case 'V':
return 22; break;
case 'W':
return 23; break;
case 'X':
return 24; break;
case 'Y':
return 25; break;
case 'Z':
return 26; break;
default:
return 0; break;
}
}
};
LeetCode(171) Excel Sheet Column Number的更多相关文章
- LeetCode(168) Excel Sheet Column Title
题目 Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...
- Leetcode Excel Sheet Column Number (C++) && Excel Sheet Column Title ( Python)
Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...
- 171. Excel Sheet Column Number(C++)
171. Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as ...
- 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; ...
- LeetCode 171. Excel表列序号(Excel Sheet Column Number) 22
171. Excel表列序号 171. Excel Sheet Column Number 题目描述 给定一个 Excel 表格中的列名称,返回其相应的列序号. 每日一算法2019/5/25Day 2 ...
- leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number
题目 //像10进制一样进行 转换 只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...
- LeetCode Excel Sheet Column Number 表列数
题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA&qu ...
- 【LeetCode】168 & 171- Excel Sheet Column Title & Excel Sheet Column Number
168 - Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...
- 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number
题目描述: Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...
随机推荐
- 继续(3n+1)猜想 (25)
#include <algorithm> #include <iostream> using namespace std; int main(){ ] = { }; ], nu ...
- inline-block元素水平居中问题
今天做项目的时候碰到了不固定元素个数,需要水平居中的问题,原来的确定宽度下margin:0 auto等方法木有用了.想起来之前看过display:inline-block的文章, 果断用这个. 之前很 ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A
Description Your search for Heidi is over – you finally found her at a library, dressed up as a huma ...
- sql基础语法-创建表和约束
创建数据库表 USE SQL2016 IF OBJECT_ID('dbo.Employees','U') IS NOT NULL DROP TABLE dbo.Employees; Create TA ...
- PowerShell和Bash的介绍
PowerShell是运行在windows平台的脚本,而Bash是运行在linux平台的脚本 现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理window ...
- Web API性能优化(一)压缩
简单的应用场景:分页获取日志JSON信息. 很简单的实现,简单的记录一下 未压缩时候 使用PostMan请求http://localhost:34390/api/gpm/syslog/page?pag ...
- mvc报( 检测到有潜在危险的 request.form 值 )错的解决方案
今天在做项目中遇到了报( 检测到有潜在危险的 request.form 值 )错,百度过后解决了该问题,出此问题主要还是因为提交的Form中有HTML字符串,例如你在TextBox中输入了html标签 ...
- 【转】JobScheduler
JobScheduler JobScheduler是Android L(API21)新增的特性,用于定义满足某些条件下执行的任务.它的宗旨是把一些不是特别紧急的任务放到更合适的时机批量处理,这样可以有 ...
- Jenkins+Ant+Jmeter搭建轻量级接口自动化
软件准备 本文所用软件版本如下: Jenkins2.176.1 Tomcat9.0.21 Ant1.9.14 Jmeter5.1.1 我已经把相应的软件上传到我的网盘中,下载地址如下: 下载链接:ht ...
- SQL Server 查询锁表和接锁表
SQL Server 查询锁表 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) as tableNa ...