【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 ...
随机推荐
- 【Python小试】计算蛋白序列中指定氨基酸所占的比例
编码 from __future__ import division def get_aa_percentage(protein, aa_list=['A','I','L','M','F','W',' ...
- git添加新账号
1,在linux上添加账号 useradd test passwd test usermod -G gitgroup test 将test账号的组改为和git一样的组gitgroup git所在 ...
- shell 指令
cat/proc/parttitions df -T pstree ext4 是linux 文件系统
- find命令常见用法
1. find linux中,find命令一般用来按特定条件查找文件,生产环境中也常用其来过滤文件 名称 find - 搜索目录层次结构中的文件 格式 find [目录] {[选项] [参数]}... ...
- JavaScript | 新手村(一)变量,运算和变量方法
资料来自:JavaScript 第一步 1. 向 html 页面添加 JavaScript 1.1 内部 JavaScript 在 html 文件中的 </body> 标签前插入代码: & ...
- 【Redis】Sentinel 哨兵模式
Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...
- [云原生]Docker - 简介
目录 什么是Docker? 为什么使用Docker? 对比传统虚拟机总结 什么是Docker? Docker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业务项目.它基于Go ...
- 【swift】Xcode未响应(卡死、卡住、CPU满载、忙碌、转圈圈)
在尝试了网上的方法,依然没能解决问题,尝试如下: 1.去自己项目的路径,找到<你的项目名.xcodeproj>,点击[显示包内容],删除xcuserdata文件夹 2.去Library,把 ...
- Android实现网络监听
一.Android Wifi常用广播 网络开发中主体会使用到的action: ConnectivityManager.CONNECTIVITY_ACTION WifiManager.WIFI_STAT ...
- Android 图片框架
1.图片框架:Picasso.Glide.Fresco 2.介绍: picasso:和Square的网络库能发挥最大作用,因为Picasso可以选择将网络请求的缓存部分交给了okhttp实现 Glid ...