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 ...
随机推荐
- 在虚拟机linux环境下编译windows版adb fastboot
原文出自:http://blog.chinaunix.net/uid-20546441-id-1746200.html 我根据虚拟机编译遇到的问题进行一些添加 [前提条件] Linux Android ...
- Gear Pump: Why Install A Pressure Reducing Valve?
When the Gear Pump Manufacturers prompts to install a gear pump, the following points should ...
- 03pandas
一.pandas简述 1)pandas是一个开源的,BSD许可的库,为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 2)numpy能够帮助我们处理数值,但是pandas除了处理数 ...
- INFORMATION_SCHEMA InnoDB 表
INFORMATION_SCHEMA InnoDB Tables 本节提供InnoDB INFORMATION_SCHEMA表的表定义. 有关相关信息和示例,请参见"InnoDB INFOR ...
- 安装完Centos 7后的一些处理
1.安装dkms:dkms-2.2.0.3-31.1.noarch.rpm 2.安装显卡驱动:amdgpu-pro-18.10-572953 3.启动图形界面使用init 5 不能使用startx
- 【HDU 6005】Pandaland(Dijkstra)
Problem Description Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can ...
- nginx.conf的完整配置说明
#用户 用户组 user www www; #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 worker_processes 5: #错误日志 error_log logs/e ...
- 大数据学习——flume拦截器
flume 拦截器(interceptor)1.flume拦截器介绍拦截器是简单的插件式组件,设置在source和channel之间.source接收到的事件event,在写入channel之前,拦截 ...
- nmon分析与详解
1.命令安装 1.查看liunx版本版本x86_64_14i 目录:cd /nmon/logs/ 版本x86_64_14i [root@localhost u06]# cd / [root@local ...
- 【转载】CentOS6.5升级手动安装GCC4.8.2
一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 1.1 获取安装包并 ...