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

思路:

相当于10进制转26进制。与一般不一样的是10进制对应的是0 - 9。而这个26进制对应的是 A(1)- Z(26), 没有0。

我的代码:

string convertToTitle(int n) {
string ans;
while(n != )
{
int num = n % ;
n /= ;
if(num != )
{
ans.push_back(num - + 'A');
}
else
{
ans.push_back('Z');
n--;
}
}
reverse(ans.begin(), ans.end());
return ans;
}

大神精简的代码:

string convertToTitle(int n) {
string res;
char tmp;
while(n){
n -= ; //这里相当于把A-Z表示成了0-25就与一般的表达一样了
tmp = 'A' + n % ;
res = tmp + res;
n /= ;
}
return res;
}

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

    A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

思路:26进制转10进制。 以AAA为例   AAA = A * 26+ A * 261 + A * 260;

int titleToNumber(string s) {
int ans = ;
int factor = ;
while(!s.empty())
{
ans = ans + (s.back() - 'A' + ) * factor;
s.pop_back();
factor *= ;
}
return ans;
}

大神精简版的:

int result = ;
for (int i = ; i < s.size(); result = result * + (s.at(i) - 'A' + ), i++);
return result;

【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)的更多相关文章

  1. 【leetcode】Remove Nth Node From End of List(easy)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. 【leetcode】Best Time to Buy and Sell 2(too easy)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. 【LeetCode】1085. Sum of Digits in the Minimum Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

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

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

  5. 【LeetCode】168 & 171- Excel Sheet Column Title & Excel Sheet Column Number

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

  6. 【leetcode】Excel Sheet Column Number

    Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appea ...

  7. 【LeetCode】171. Excel Sheet Column Number

    题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...

  8. Excel Sheet Column Title&&Excel Sheet Column Number

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

  9. 【LeetCode】Algorithms 题集(三)

    Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...

  10. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

随机推荐

  1. Javascript 方法大全

    一.基础知识 1 创建脚本块 1: <script language=”JavaScript”> 2: JavaScript code goes here 3: </script&g ...

  2. 第二章平稳时间序列模型——AR(p),MA(q),ARMA(p,q)模型及其平稳性

      1白噪声过程: 零均值,同方差,无自相关(协方差为0) 以后我们遇到的efshow如果不特殊说明,就是白噪声过程. 对于正态分布而言,不相关即可推出独立,所以如果该白噪声如果服从正态分布,则其还将 ...

  3. OFFICE文档(DOC,XLS,PPT)打开报错的解决办法!

    一般情况下,打开OFFICE文档报错都是因为模板文件出错!! 至于为什么会出错这个问题不好说,可能是不正确关闭文档等等,重装OFFICE也不一定能解决问题! 出现这种情况一般是所有的Word文档或者E ...

  4. maven 向本地私库导入jar

    mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> - ...

  5. zstu.2512. Moving Tables(贪心)

     Moving Tables Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1182  Solved: 563 Description The famo ...

  6. Inno Step 安装包程序 写INI配置文件脚本

    [INI]Filename: "{app}\Config\config.ini"; Section: "config";Key: "name" ...

  7. Javascript高级程序设计——函数

    函数Function 通过函数封装多条语句,在任何地方执行.javascript函数不会重载,相同名字函数,名字属于后定义的函数通过function关键词声明. function functionNa ...

  8. crontab用法与实例

    导读 在Linux系统的实际使用中,可能会经常碰到让系统在某个特定时间执行某些任务的情况,比如定时采集服务器的状态信息.负载状况:定时执行某些任务/脚本来对远端进行数据采集等.这里将介绍下cronta ...

  9. CAniamtion 基本使用

    CAAnimation(抽象)<NSCoding, NSCopying, CAMediaTiming, CAAction> QuartzCore框架的基本继承结构 -> CATran ...

  10. Longest Common Substring

    Given two strings, find the longest common substring. Return the length of it. Example Given A = &qu ...