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. IIS 您要访问的网页有问题,无法显示!

    提示:您要访问的网页有问题,无法显示.HTTP 500 – 内部服务器错误.”的问题解决方案! IIS服务器出现错误的原因很多,请尝试以下操作:1.查看网站属性——文档看看启用默认文档中是否存在:in ...

  2. 微信公众平台回复链接可以直接访问,但不能是锚文字链接<a>标签

    最近在学习微信公众平台开发,由于编辑模式和开发模式不可同时开启,在开发模式下如果访客发送关键字过来暂时无法实现关键词自动回复,客服人员先用链接网址直接回复订阅用户,但请注意不能是文字链接,即<a ...

  3. tc 147 2 PeopleCircle(再见约瑟夫环)

    SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...

  4. Android在TextView中实现RichText风格

    参考: Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格 Demo: private SpannableStringBuilder c ...

  5. iOS中AOP与Method Swizzling 项目中的应用

    引子:项目中需要对按钮点击事件进行统计分析,现在项目中就是在按钮的响应代码中添加点击事件,非常繁琐.所以使用了AOP(面向切面编程),将统计的业务逻辑统一抽离出来. 项目中添加的开源库:https:/ ...

  6. java之google style

    Google的Java编码规范英文版: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html Google的Java编码规范 ...

  7. JAVA设计模式 之 策略模式

    一. 定义 设计模式定义了算法族,分别封装起来,让他们之间可以互相替代,此模式让算法的变化独立于使用算法的客户(该定义来自于Head First 设计模式). 二. 应用场景 当我们在应用程序中完成一 ...

  8. Intent flag 与启动模式的对应关系

    Activity有四种启动模式: 1.standard(标准)    2.singleTop    3.singleTask  4.singleInstance 标识某个Activity的启动模式,有 ...

  9. django的信号

    Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 1.Django内置信号 Model signals pre_in ...

  10. pycharm3.4 下svn 项目checkout&配置

    pycharm 社区版: 3.4 1. checkout 项目 注意,之前配置好:设置里面的一些配置:(以下勾勾不要勾上) 2. checkout 项目之后,做以下操作: vcs ->enabl ...