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. 常用的sql语句(存储过程语法)

    1.存储过程语法 ①package create or replace package PKG_RPT_WAREHOUSE is -- Author : -- Created : 2018/9/28 ...

  2. 成为Java顶尖程序员 ,看这11本书就够了

    以下是我推荐给Java开发者们的一些值得一看的好书.但是这些书里面并没有Java基础.Java教程之类的书,不是我不推荐,而是离我自己学习 Java基础技术也过去好几年了,我学习的时候看的什么也忘了, ...

  3. Spark学习笔记4:数据读取与保存

    Spark对很多种文件格式的读取和保存方式都很简单.Spark会根据文件扩展名选择对应的处理方式. Spark支持的一些常见文件格式如下: 文本文件 使用文件路径作为参数调用SparkContext中 ...

  4. 基于Vue的Ui框架

    基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 http://element.eleme.io/ MintUi 基于vue 移动 ...

  5. direct path read/write (直接路径读/写)

    转载:http://www.dbtan.com/2010/04/direct-path-readwrite.html direct path read/write (直接路径读/写): 直接路径读(d ...

  6. IO流程及优化

    http://blog.csdn.net/xypzwl/article/details/51416883 一.存储设备的存储原理 机械硬盘: 机械硬盘使用磁性物质作为存储介质,用N.S极性来代表0或1 ...

  7. 开发组件:Systemd

    Systemd 入门教程:命令篇 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

  8. InterlliJ IDEA 2017.3.x / 2017.3.4 License Server激活

    InterlliJ IDEA 2017.3.x / 2017.3.4 License Server激活 1.Lincense Server激活 // 激活IDEA的License Server 地址 ...

  9. Tomcat组成与工作原理

    laosijikaichele 关注  0.2 2018.06.02 10:44 字数 5175 阅读 2798评论 0喜欢 8 原文:https://juejin.im/post/58eb5fdda ...

  10. zabbix 监控数据库 及 tcp连接数

    参考网站: 监控sqlserver      http://blog.51cto.com/13272050/2050601 http://blog.51cto.com/ultrasql/1927672 ...