【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)
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 * 262 + 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)的更多相关文章
- 【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 ...
- 【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 ...
- 【LeetCode】1085. Sum of Digits in the Minimum Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【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】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 ...
- 【leetcode】Excel Sheet Column Number
Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appea ...
- 【LeetCode】171. Excel Sheet Column Number
题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...
- 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】Algorithms 题集(三)
Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...
- 【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 ...
随机推荐
- C# Winform 脱离 Framework (一)
Linker是一个命令行工具,它以将我们的.net程序生成可脱离.net framework环境运行的程序 . Linker不支持中文的路径,在程序中也不能有中文的标识符. Linker 有2种部署方 ...
- Tomcat 6 —— Realm域管理
本篇来源于官方文档,但不仅仅是翻译,其中不乏网上搜索的资料与自己的理解. 如有错误,请予指正. 什么是Realm 首先说一下什么是Realm,可以把它理解成“域”,也可以理解成“组”,因为它类似 类U ...
- WCF服务显示的是服务器名称而不是IP地址...
打开http://xx.xx.xx.xx:端口号/Service1.svc页面显示的服务地址为: http://xx_yy_server:端口号/Service1.svc?wsdl 是显示的服务器的名 ...
- RGB颜色矩提取算法——Matlab
一.颜色矩公式 一阶颜色矩——均值,反映图像明暗程度 二阶颜色矩 ——标准差,反映图像颜色分布范围 三阶颜色矩 ——方差,反映图像颜色分布对称性 二.方法一: firstMoment = mean(m ...
- 读取XML文件
首先要确定好XML文件的位置,最好是放在程序的debug文件中,放在其他地方也可以,要写上绝对路径 using System; using System.Collections.Generic; us ...
- 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡【转载】
文章 原始出处 :http://sofar.blog.51cto.com/353572/1601552 ============================================== ...
- java之BASE64加解密
1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...
- linux 输出重定向一份到本地文件,屏幕继续输出
ls -al 2>&1 | tee xLog
- 弹窗插件 popup.js 完美修正版
作为信息展示弹出窗口,很有用!是一个js插件,不是jQuery插件! 地址:http://img.jb51.net/online/popup/popup.html
- QQ空间个人中心的广告
http://qzonestyle.gtimg.cn/qzone/space_item/boss_pic/*.jpghttp://img*.paipaiimg.com/*.jpghttp://cn.q ...