LeetCode 168 Excel Sheet Column Title(Excel的列向表标题)
翻译
给定一个正整数,返回它作为出如今Excel表中的正确列向标题。
比如:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
原文
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
分析
我非常少用Excel。所以题意不是满懂,就先看了看别人的解法。
class Solution {
public:
string convertToTitle(int n) {
if (n<=0) return "";
if (n<=26) return string(1, 'A' + n - 1);
return convertToTitle( n%26 ?
n/26 : n/26-1 ) + convertToTitle( n%26 ? n%26 : 26 );
}
};
然后放到VS中试了一下。
最终知道题目的意思了……
1 ... A
*******
26 ... Z
27 ... AA
*******
52 ... AZ
53 ... BA
*******
702 ... ZZ
703 ... AAA
大神的递归用的真是666,三目运算符也用的恰到优点。不得不佩服呐!
上面採用的是
'A' + n - 1
紧接着。我写了例如以下代码:
#include <iostream>
using namespace std;
string convertToTitle(int n) {
string title;
while (n > 0) {
title = (char)('A' + (--n) % 26) + title;
n /= 26;
}
return title;
}
int main() {
cout << convertToTitle(702);
return 0;
}
核心代码
title = (char)('A' + (--n) % 26) + title;
解决的是习惯上都用的是
string title;
title += "X";
这样都是往末尾追加的,可能在前面追加不是非常习惯,只是也是没问题的。
由于有个起始的A在里面,所以后面加的时候一開始要把n减掉1。每次得到最后一个字符,也就是每次除掉一个26,就当作是26进制一样,事实上和十进制是相通的。
代码
class Solution {
public:
string convertToTitle(int n) {
string res;
while (n > 0) {
res = (char)('A' + (--n) % 26) + res;
n /= 26;
}
return res;
}
};
LeetCode 168 Excel Sheet Column Title(Excel的列向表标题)的更多相关文章
- 【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 Title & Excel Sheet Column Number
题目描述: Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...
- Excel Sheet Column Title & Excel Sheet Column Number
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- 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】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 exa ...
- 168 Excel Sheet Column Title Excel表列名称
给定一个正整数,返回它在Excel表中相对应的列名称.示例: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -&g ...
- LeetCode 168. Excel表列名称(Excel Sheet Column Title)
168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...
- 【leetcode】Excel Sheet Column Title
Excel Sheet Column Title Given a non-zero positive integer, return its corresponding column title as ...
- 168. Excel Sheet Column Title
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- 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 ...
随机推荐
- spring注解开发-AOP(含原理)
一:AOP基本使用 AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式: 步骤一:导入aop模块:Spring AOP:(spring-aspects) <depen ...
- [LOJ] 分块九题 4
https://loj.ac/problem/6280 区间修改,区间求和. 本来线段树的活. //Stay foolish,stay hungry,stay young,stay simple #i ...
- mysql两种常用备份工具
一.mysqldump备份: 在开启GTID模式时,在master上执行的备份: --set-gtid-purged=OFF 在开启GTID模式时,要在slave上执行,想重新搭建一套slave环境. ...
- HTML5编辑API之Range对象
Range对象代表页面上的一段连续区域,通过Range对象,可以获取或修改页面上的任何区域,可以通过如下创建一个空的Range对象,如下: var range = document.createRa ...
- 剑指Offer(书):数组中重复的数字
题目:找出数组中重复的数字. 说明:在一个长度为n的数组里的所有数字都在0~n-1的范围内,数组中某些数字是重复的,但是不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...
- 【UVA 11181】(条件概率)
题链:https://cn.vjudge.net/problem/UVA-11181 题意 n个人去了超市,已知每个人买东西的概率为p[i],在已知有r个人买了东西的情况下,求实际上每个人买东西的概率 ...
- 3. express 框架使用 vue框架 weiUI
express 1. 安装 npm install express --save 2. 创建项目 vue js 安装Vuejs vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue- ...
- hust训练赛20160330--B - 又见LKity
Problem 2122 又见LKity Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 嗨!大家好,在Templ ...
- hihoCoder#1139 二分·二分答案
原题地址 挺简单一道题,结果因为一时傻逼写错一个小地方,导致WA成狗了_(:з」∠)_ 代码: #include <iostream> #include <cstring> # ...
- 洛谷P2527 [SHOI2001]Panda的烦恼
题目描述 panda是个数学怪人,他非常喜欢研究跟别人相反的事情.最近他正在研究筛法,众所周知,对一个范围内的整数,经过筛法处理以后,剩下的全部都是质数,不过panda对这些不感兴趣,他只对被筛掉 ...