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 ...
随机推荐
- [转]NSIS:判断D盘存在与否确定安装路径
转载自:http://www.flighty.cn/html/bushu/20140704_239.html 现在我们想实现这样的功能: 如果目标机器存在D盘,那么就安装程序到D盘,否则安装在系统 ...
- javascript中有关this的解析题
1.作用域链 作用域:浏览器给js一个生存环境(栈)内存 作用域链:js中的关键字var function 都可以提前声明和定义,提前声明和定义,放在我们的内存地址(堆)内存中,然后js从上到下逐行执 ...
- php 学习笔记 设计和管理
代码管理 文件路径.数据库名.密码禁止 hard coded 避免重复代码在多个页面复制粘贴 Gang of Four eXtreme Programming 的主要原则是坚决主张测试是项目成功的关键 ...
- opencv小问题大智慧
opencv易错点记录 1. 判断条件不应使用CV_ASSERT(),而使用CV_Assert(). 2. 用下面这个自适应阈值必须进行数据的转换,不能直接传入数据!不然程序一直崩溃,找了很久才发现 ...
- c++官方文档-动态内存
#include<iostream> #include <new> using namespace std; int main() { /** * 动态内存 * url: ht ...
- python学习中的第一个例子
搭建python 先学习下当小白鼠 1 看下自己的python版本 python -v 2 然后,用pip安装开发Web App需要的第三方库: 异步框架aiohttp: pip3 install a ...
- placeholder测试
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 《汇编语言 基于x86处理器》前五章的小程序
▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...
- 将文本转换为json的工具类
JSONObject jsonObj = JSONObject.fromObject("文本"); 参考:https://www.cnblogs.com/joahyau/p/ ...
- leetcode447
public class Solution { /// <summary> /// 计算两个点的距离 /// </summary> /// <param name=&qu ...