博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode No.168
阅读量:4532 次
发布时间:2019-06-08

本文共 1148 字,大约阅读时间需要 3 分钟。

给定一个正整数,返回它在 Excel 表中相对应的列名称。

例如,

1 -> A

2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
示例 1:

输入: 1

输出: "A"
示例 2:

输入: 28

输出: "AB"
示例 3:

输入: 701

输出: "ZY"

 

解题思路:构建map容器用于存储显示的字符。本题通过 number 除以26得到的 remainder,即得到最后一位的字符。然后将 quotient 赋值给 number,依次循环。

需要注意的是边界字符,即如 26、52之类。所以需要加一个判断条件,即当 quotient 大于0且 remainder 为0时,需要 quotient - 1,并且添加字符 Z

//168string convertToTitle(int n){    if(n<1) return "";    map
m{ {
1,'A'},{
2,'B'},{
3,'C'},{
4,'D'},{
5,'E'},{
6,'F'},{
7,'G'},{
8,'H'},{
9,'I'},{
10,'J'}, {
11,'K'},{
12,'L'},{
13,'M'},{
14,'N'},{
15,'O'},{
16,'P'},{
17,'Q'},{
18,'R'},{
19,'S'},{
20,'T'}, {
21,'U'},{
22,'V'},{
23,'W'},{
24,'X'},{
25,'Y'},{
26,'Z'} }; string res; int quotient = n, reminder; while(quotient>0) { reminder=quotient%26; quotient=quotient/26; if(quotient>0 && reminder==0) { quotient -=1; res ='Z' + res; } else res = m[reminder] + res; } return res;}//168

 

转载于:https://www.cnblogs.com/2Bthebest1/p/11181869.html

你可能感兴趣的文章
ORACLE 建库过程总结
查看>>
Ogre1.8.1 Basic Tutorial 6 - The Ogre Startup Sequence
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用...
查看>>
c# Winform 开发分屏显示应用程序
查看>>
canvas刮奖
查看>>
添加源ubuntu_x64 安装 Adobe Reader
查看>>
给datalist加自动编号(解决博客的第XX楼)
查看>>
BZOJ3282: Tree (LCT模板)
查看>>
ES6中变量的解构赋值
查看>>
数据绑定控件Reperter
查看>>
【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)
查看>>
Yii DataProvider
查看>>
BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
查看>>
hdu 1114 Piggy-Bank
查看>>
maven集成tomcat插件启动报错
查看>>
Boost库编译安装
查看>>
算法复习——数位dp(不要62HUD2089)
查看>>
Spark2.1.0——运行环境准备
查看>>
noip模拟赛 寻宝之后
查看>>
ZOJ2833*(并查集)
查看>>