LeetCodeOJ_168_Excel Sheet Column Title
2015-07-20 12:26
381 查看
答题链接
For example:
1 -> A
2 -> B
3 -> C
…
26 -> Z
27 -> AA
28 -> AB
class Solution {
public:
string convertToTitle(int n) {
string str;
string c;
int m=n;
while(n>0)
{
m=n%26;
if(m==0)
m=26;
n=(n-m)/26;
switch(m)
{
case 1:{ c=”A”; break;}
case 2:{ c=”B”; break;}
case 3:{ c=”C”; break;}
case 4:{ c=”D”; break;}
case 5:{ c=”E”; break;}
case 6:{ c=”F”; break;}
case 7:{ c=”G”; break;}
case 8:{ c=”H”; break;}
case 9:{ c=”I”; break;}
case 10:{ c=”J”; break;}
case 11:{ c=”K”; break;}
case 12:{ c=”L”; break;}
case 13:{ c=”M”; break;}
case 14:{ c=”N”; break;}
case 15:{ c=”O”; break;}
case 16:{ c=”P”; break;}
case 17:{ c=”Q”; break;}
case 18:{ c=”R”; break;}
case 19:{ c=”S”; break;}
case 20:{ c=”T”; break;}
case 21:{ c=”U”; break;}
case 22:{ c=”V”; break;}
case 23:{ c=”W”; break;}
case 24:{ c=”X”; break;}
case 25:{ c=”Y”; break;}
case 26:{ c=”Z”; break;}
}
str.insert(0,c.c_str());
}
return str;
}
};
题目:
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
代码:
“`class Solution {
public:
string convertToTitle(int n) {
string str;
string c;
int m=n;
while(n>0)
{
m=n%26;
if(m==0)
m=26;
n=(n-m)/26;
switch(m)
{
case 1:{ c=”A”; break;}
case 2:{ c=”B”; break;}
case 3:{ c=”C”; break;}
case 4:{ c=”D”; break;}
case 5:{ c=”E”; break;}
case 6:{ c=”F”; break;}
case 7:{ c=”G”; break;}
case 8:{ c=”H”; break;}
case 9:{ c=”I”; break;}
case 10:{ c=”J”; break;}
case 11:{ c=”K”; break;}
case 12:{ c=”L”; break;}
case 13:{ c=”M”; break;}
case 14:{ c=”N”; break;}
case 15:{ c=”O”; break;}
case 16:{ c=”P”; break;}
case 17:{ c=”Q”; break;}
case 18:{ c=”R”; break;}
case 19:{ c=”S”; break;}
case 20:{ c=”T”; break;}
case 21:{ c=”U”; break;}
case 22:{ c=”V”; break;}
case 23:{ c=”W”; break;}
case 24:{ c=”X”; break;}
case 25:{ c=”Y”; break;}
case 26:{ c=”Z”; break;}
}
str.insert(0,c.c_str());
}
return str;
}
};
结果:
相关文章推荐
- 使用testng listener实现测试用例失败重跑功能
- 前言
- [LeetCode][Java] Decode Ways
- A20平台增加camera
- Android CollapsingToolbarLayout
- MSSQL- select @@identity的用法
- Check if you are behind a transparent proxy
- jquery 自定义事件
- MySQL学习笔记——多表连接和子查询
- 使用 Eclipse 调试 Java 程序的 10 个技巧
- Zabbix监控LVS状态及keepalived VIP漂移
- Zabbix监控LVS状态及keepalived VIP漂移
- Zabbix监控LVS状态及keepalived VIP漂移
- Django模型修改及数据迁移
- AppStore:四步教你申请iOS公司开发者账号
- S5PV210开发系列六_Fatfs的移植
- Shortcut的创建
- SWFObject基本用法实例分析
- 高斯消元模板整理
- guava cache编程实践总结