【LeetCode】168. Excel Sheet Column Title 解题报告(Java & Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/excel-sheet-column-title/
Total Accepted: 59514 Total Submissions: 274188 Difficulty: 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
...
Example 1:
Input: 1
Output: "A"
Example 2:
Input: 28
Output: "AB"
Example 3:
Input: 701
Output: "ZY"
题目大意
迭代
和之前的罗马数很像嘛!
转换数字然后往右移位。主要问题是26这个数字和0效果上是一样的,除以26都是剩余0,我做了单独讨论。第二,中间用到了字符强转和重新构造字符串。这些都比较耗时。
Java解法如下:
public class Solution {
public String convertToTitle(int n) {
String answer="";
while( n > 0){
int temp= n % 26;
if(temp==0){
answer="Z" + answer;
n=n/26-1;
}else{
answer=new String(Character.toChars('A'+temp-1)) + answer;
n/=26;
}
}
return answer;
}
}
AC:0ms
Python解法如下:
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
if n == 1: return 'A'
res = ""
while n:
if n % 26 == 0:
res = "Z" + res
n -= 26
else:
res = chr(ord('A') - 1 + n % 26) + res
n -= n % 26
n /= 26
return res
C++代码如下,主要思路是一样的,但是不能直接使用char + string, 但是string + char是可以的。因为string作为一个类,重载了operator+(char)方法,但是char是原始数据类型,他没有重载operator+(string)方法。
所以最后需要reverse。
class Solution {
public:
string convertToTitle(int n) {
string res;
while (n != 0) {
if (n % 26 == 0) {
res += "Z";
n -= 26;
} else {
res += (n % 26) - 1 + 'A';
n -= n % 26;
}
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
};
递归
感觉自己方法不够好,看到了九章算术这个方法,我认为很好。n-1的效果是算法上每个循环都是从0–25,这样使程序对齐,简化了运算。
public class Solution {
public String convertToTitle(int n) {
if (n == 0) {
return "";
}
return convertToTitle((n - 1) / 26) + (char)((n - 1) % 26 + 'A');
}
}
AC:0ms
日期
2016/4/30 16:20:51
2018 年 11 月 28 日 —— 听说楼下有传染病。。
【LeetCode】168. Excel Sheet Column Title 解题报告(Java & Python & C++)的更多相关文章
- [LeetCode] 168. Excel Sheet Column Title 求Excel表列名称
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number
题目 //像10进制一样进行 转换 只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...
- Java for LeetCode 168 Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- Java [Leetcode 168]Excel Sheet Column Title
题目描述: Given a positive integer, return its corresponding column title as appear in an Excel sheet. F ...
- 【原创】leetCodeOj --- Excel Sheet Column Title 解题报告
题目地址: https://oj.leetcode.com/problems/excel-sheet-column-title/ 题目内容: Given a positive integer, ret ...
- LeetCode 168. Excel Sheet Column Title (Excel 表格列名称)
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- LeetCode 168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. -> A ...
- ✡ leetcode 168. Excel Sheet Column Title 26进制数字 --------- java
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- LeetCode 171 Excel Sheet Column Number 解题报告
题目要求 Given a column title as appear in an Excel sheet, return its corresponding column number. For e ...
随机推荐
- FESTUNG — 3. 采用 HDG 方法求解对流问题
FESTUNG - 3. 采用 HDG 方法求解对流问题[1] 1. 控制方程 线性对流问题控制方程为 \[\begin{array}{ll} \partial_t c + \nabla \cdot ...
- Yii2 源码分析 入口文件执行流程
Yii2 源码分析 入口文件执行流程 1. 入口文件:web/index.php,第12行.(new yii\web\Application($config)->run()) 入口文件主要做4 ...
- SpringBoot整合Shiro 二:Shiro配置类
环境搭建见上篇:SpringBoot整合Shiro 一:搭建环境 Shiro配置类配置 shiro的配置主要集中在 ShiroFilterFactoryBean 中 关于权限: anon:无需认证就可 ...
- 转 onSaveInstanceState()和onRestoreInstanceState()使用详解
转 https://www.jianshu.com/p/27181e2e32d2 背景 如果系统由于系统约束(而不是正常的应用程序行为)而破坏了Activity,那么尽管实际 Activity实例已经 ...
- 【Linux】【Basis】块存储,文件存储,对象存储
1. 块存储: 定义:这种接口通常以QEMU Driver或者Kernel Module的方式存在,这种接口需要实现Linux的Block Device的接口或者QEMU提供的Block Driver ...
- go channel 概述 - 管道
概述 unix/linux OS 的一个进程的输出可以是另一个进程的输入,这些进程使用stdin与stdout设备作为通道,在进程之间传递数据. 同样的,GO中有io.Reader与io.Writer ...
- RTTI (Run-time type information) in C++
In C++, RTTI (Run-time type information) is available only for the classes which have at least one v ...
- OpenStack之六: plancement服务(端口8778)
官网地址:https://docs.openstack.org/placement/stein/install/install-rdo.html #:创建placement库,并授权 MariaDB ...
- "delete this" in C++
Ideally delete operator should not be used for this pointer. However, if used, then following points ...
- Linux系统的负载与CPU、内存、硬盘、用户数监控的shell脚本
利用Shell脚本来监控Linux系统的负载.CPU.内存.硬盘.用户登录数. 这几天在学习研究shell脚本,写的一些系统负载.CPU.内存.硬盘.用户数监控脚本程序.在没有nagios监控的情况下 ...