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 ...
随机推荐
- selenium java-3 定位元素的八种方法
web driver提供了八种元素定位的方法: id name class name tag name link text partial link text xpath css selector 如 ...
- vertical-align表单元素垂直对齐
原文地址:http://www.blueidea.com/tech/web/2009/6910.asp 最近的项目涉及到很多表单的制作,特别是复选框(checkbox)和单选框(radio).但是在前 ...
- mysql 不能插入中文和显示中文
一)不能显示中文解决办法: 参考:http://bbs3.chinaunix.net/thread-880131-1-1.html 1:windows平台,因为windows操作系统默认的是用了gb2 ...
- java字符串分解 StringTokenizer用法
Java中substring方法可以分解字符串,返回的是原字符串的一个子字符串.如果要讲一个字符串分解为一个一个的单词或者标记,StringTokenizer可以帮你. 先看个例子: 1 public ...
- django中使用Ajax
内容: 1.Ajax原理与基本使用 2.Ajax发送get请求 3.Ajax发送post请求 4.Ajax上传文件 5.Ajax设置csrf_token 6.django序列化 参考:https:// ...
- zabbix微信报警
[root@LinuxS04 jiaoben]# ./weixin 联系人 baojing baojingok[root@LinuxS04 jiaoben]# pwd/usr/local/zabbix ...
- FMX ScrollBox 拖拽控制
Firemonkey下的ScrollBox 拖拽控制,滚动控制,拖拽,滚动条 AniCalculations 仅允许纵向拖拽,拖拽 scrlbx.AniCalculations.TouchTracki ...
- python 之九九乘法表
for i in range(1,10): for j in range(1,i+1): print(f"{j}*{i}={i*j}",end='\t') print() 运行结果 ...
- Java synchronized(this)锁住的是什么
synchronized锁住的是括号里面的对象,而不是代码. 对于非static的synchronized方法,锁的就是对象本身,也就是this.
- autolayout UILabel 设置最大宽度
label1.preferredMaxLayoutWidth = 100: label1.numberOfLines = 0; //自适应行数