【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 ...
随机推荐
- Augustus指南(Trainning部分)
Augustus指南 官方 Tutorial Index Augustus是一个真核生物基因预测软件,目前有网页服务端和本地版,它基于Hidden-Markov Model(隐马尔科夫链模型HMM)( ...
- jQuery ajax常用示例
总结一下jQuery ajax常用示例 $.ajax({ type: "post", //类型get,post url: urls, //链接地址 data:{"id&q ...
- hadoop/spark面试题
总结于网络 转自:https://www.cnblogs.com/jchubby/p/5449379.html 1.简答说一下hadoop的map-reduce编程模型 首先map task会从本地文 ...
- Spark Stage 的划分
Spark作业调度 对RDD的操作分为transformation和action两类,真正的作业提交运行发生在action之后,调用action之后会将对原始输入数据的所有transformation ...
- Oracle—表、约束、索引、表空间、分区、序列、统计信息
表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...
- Oracle——生成Awr报告
Oracle--生成Awr报告 AWR的概念 Oracle数据库是一个使用量很多的数据库,关于Oracle数据库的性能.Oracle10g以后,Oracle提供了一个性能检测的工具:AWR(Autom ...
- 100个Shell脚本——【脚本2】截取字符串
[脚本2]截取字符串 一.脚本 现有一个字符串如下: http://www.aaa.com/root/123.htm 请根据以下要求截取出字符串中的字符: 1.取出www.aaa.com/root/1 ...
- GO并发相关
锁的使用 注意要成对,重点是代码中有分支或者异常返回的情况,这种情况要在异常返回前先释放锁 mysqlInstanceLock.Lock() slaveHostSql := "show sl ...
- Nginx+ uWSGI +django进行部署
一:uWSGI的安装 sudo pip install uwsgi 如果安装报错: conda install -c conda-forge uwsgi conda install -c conda- ...
- matplotlib画3d图
import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D fig = plt.f ...