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 ...
随机推荐
- process data
# version 1.0def connect_mysql(sql, oper_type="select", data_l=None): conn = pymysql.conne ...
- source 1.7 中不支持 lambda 表达式(请使用 -source 8 或更高版本以启用 lambda 表达式)
from:http://blog.csdn.net/qwdafedv/article/details/54691740 https://www.youtube.com/watch?v=oXxijdf8 ...
- kubeadm1.14.1 安装Metrics Server
Metrics API 介绍Metrics-Server之前,必须要提一下Metrics API的概念 Metrics API相比于之前的监控采集方式(hepaster)是一种新的思路,官方希望核心指 ...
- ES搭建
https://www.cnblogs.com/jstarseven/p/6803054.html
- SVN 初级教程
版本控制器:SVN 1.SVN 作用? 备份.代码还原.协同修改.多版本项目文件管理.追溯问题代码的编写人和编写时间.权限控制等. 2.版本控制简介 2.1 版本控制[Revision control ...
- Django框架基础知识14-类视图
MTV view视图 wsgi函数 def index(request): .... return HttpResponse() 特定的HTTP方法.get,post可以定义单独的方法 继承,多继承, ...
- 第六天,字典Dictionary
字典(Dictionary)在Python中是一种可变的容器模型,它是通过一组键(key)值(value)对组成,这种结构类型通常也被称为映射,或者叫关联数组,也有叫哈希表的.每个key-value之 ...
- JVM——内存管理和垃圾回收
1. 何为GC 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/51892567 Java与C语言相比的一个优势是,可以通过自己的JV ...
- xmind 8 readme
xmind 8 readme README LICENSE XMind 3 is dual licensed under 2 open source licenses: the Ecl ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...