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)的更多相关文章

  1. 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number

    题目描述: Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...

  2. 【leetcode】Excel Sheet Column Title

    Excel Sheet Column Title Given a non-zero positive integer, return its corresponding column title as ...

  3. Excel Sheet Column Title & Excel Sheet Column Number

    Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...

  4. 168. Excel Sheet Column Title

    Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...

  5. 2016.5.19——Excel Sheet Column Title

    Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[ ...

  6. 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 ...

  7. Excel Sheet Column Title&&Excel Sheet Column Number

    Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...

  8. LeetCode 168. Excel表列名称(Excel Sheet Column Title)

    168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...

  9. LeetCode_168. Excel Sheet Column Title

    168. Excel Sheet Column Title Easy Given a positive integer, return its corresponding column title a ...

随机推荐

  1. 开发框架-APP:Hybird App

    ylbtech-开发框架-APP:Hybird App Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体 ...

  2. [转]C# 使用代理访问网络

    本文部分内容来自:https://zhidao.baidu.com/question/563196409.html 也可以参考:http://www.cnblogs.com/stuart/p/5442 ...

  3. cookie js案例

    //存cokie function setcookie(keys,value,time){ document.cookie=keys+"="+decodeURIComponent( ...

  4. [UE4]Selector和Sequence的区别

    Selector和Sequence子节点都是返回true才会执行下一个子节点. Sequence是从左到右依次执行,左边节点如果返回false,则不会执行右边的节点 Selector会同步执行所有子节 ...

  5. SQL Server 2008 CDC增量变更捕获详解

    1 背景: 随着公司业务的成长,数据量也随之的不断增长.随之而来的问题是在做ETL的时候,时间花费也越来越长.为了节省时间开销,我们只想要更新最新的数据,不想要把公司历年所有的数据都进行处理.这种情况 ...

  6. SDOI 2017 天才黑客

    /* 根据claris的 博客以及 beginend 的博客来写的 首先考虑如何求出最短路 可以从样例看出 路径是从边走到边的, 所以我们将边看作点 有共同端点的两边之间 互相连边, 边权为lcp. ...

  7. php读取word里面的内容antiword

    其实是现在一个linux下的扩展 1 先安装  antiword yum antiword install 2 写测试php代码 header("Content-type: text/htm ...

  8. OpenACC数据管理语句

    ▶ 书中第4章,数据管理部分的代码和说明 ● 代码,关于 copy,copyin,copyout,create #include <stdio.h> #include <openac ...

  9. saltstack安装配置及常用命令

    1.salt安装及配置详解 https://www.cnblogs.com/lgeng/p/6567424.html centos7配置: https://www.jianshu.com/p/4c91 ...

  10. CentOs - 使用ssh key远程登录

    环境: 服务器端CentOs,本地OS X 服务器端: 1. 安装openssl使实现ssl协议 2. 将本地的pub key加入信任列表 本地: 1. 生成pub key 2. 配置ssh别名使登陆 ...