Excel Sheet Column Title (STRING - TYPE CONVERTION)
QUESTION
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
FIRST TRY
class Solution {
public:
string convertToTitle(int n) {
int remain = n%;
n /= ;
string ret = "";
char ch;
while()
{
if(n == && remain == )
{
return ret;
}
else if(n == )
{
ch = getChar(remain);
return ch + ret;
}
ch = getChar(remain);
ret = ch + ret; //char -> string, direct concat
remain = n%;
n /= ;
}
return ret;
}
char getChar(int n)
{
if(n != )
return 'A'+ n - ;
else
return 'Z';
}
};
Result: Wrong
Input: 26
Output: "AZ"
Expected: "Z"
SECOND TRY
注意了余数为0的情况
class Solution {
public:
string convertToTitle(int n) {
int remain = n%;
n /= ;
string ret = "";
char ch;
while()
{
if(n == && remain == )
{
return ret;
}
else if(n == )
{
ch = getChar(n,remain);
return ch + ret;
}
ch = getChar(n,remain);
ret = ch + ret; //char -> string, direct concat
remain = n%;
n /= ;
}
return ret;
}
char getChar(int& n, int remain)
{
if(remain != )
return 'A'+ remain - ;
else
{
n -= ;
return 'Z';
}
}
};
Excel Sheet Column Title (STRING - TYPE CONVERTION)的更多相关文章
- 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number
题目描述: Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...
- 【leetcode】Excel Sheet Column Title
Excel Sheet Column Title Given a non-zero positive integer, return its corresponding column title as ...
- Excel Sheet Column Title & Excel Sheet Column Number
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- 168. Excel Sheet Column Title
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- 2016.5.19——Excel Sheet Column Title
Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[ ...
- 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 ...
- Excel Sheet Column Title&&Excel Sheet Column Number
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- LeetCode 168. Excel表列名称(Excel Sheet Column Title)
168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...
- LeetCode_168. Excel Sheet Column Title
168. Excel Sheet Column Title Easy Given a positive integer, return its corresponding column title a ...
随机推荐
- shell 5参数
shell传递参数 我们可以在执行shell脚本时,向脚本传递参数. $n n代表数字.0表示执行的脚本名称,1表示第1个参数,2是第2个参数 $# 传递到脚本的参数个数 $$ 脚本运行的当前进程的I ...
- linux shell 命令常用快捷键
下面是一些shell的常用快捷键,快捷键玩熟悉了在一定程度上是可以提高工作效率滴… Ctrl + a 切换到命令行开始 Ctrl + e 切换到命令行末尾 Ctrl + l 清除屏幕内容 Ctrl + ...
- 阿里云启用IPV6
ping过别人的IPv6网址之后,可以确定,局域网是不支持IPv6的.所以要使用隧道技术建立两台机器之间的IPv6连接 1.发现测试用服务器上没有IPv6地址.所以测试服务器的内核应该是没有IPv6模 ...
- http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html
http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html
- /PROC/MEMINFO之谜
网站转自:http://linuxperf.com/?p=142 非常技术的网站,够看上一阵子的(一篇文章) /proc/meminfo是了解Linux系统内存使用状况的主要接口,我们最常用的”fre ...
- Missing write access to /usr/local/lib/node_modules/webpack/node_modules/assert
1. 加上sudo指令 sudo npm install ... 2. 可能是网络原因, 改用cnpm cnpm install ...
- Python之模块(二)
1.subprocess模块 2.loggin模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日 ...
- Servlet、Servlet容器等内容讲解
转载自http://blog.csdn.net/iAm333 对于Servlet.Servlet容器以及一个Servlet容器-Tomcat这些概念讲解的挺清晰的,转载下 之前在开源中国看到一篇文章& ...
- selenium+python自动化86-Chrome正在受到自动软件的控制
出现问题 1.用selenium启动浏览器出现'Chrome正在受到自动软件的控制' 2.如果不想看到这种讨厌的提示语,启动浏览器时候加个配置就行了 disable-infobars 1.在浏览器配置 ...
- rsync同步web数据
rsync远程同步web服务器的数据 实验拓扑 服务器A(rsync服务器)--------------服务器B( ...