首页 > 学院 > 开发设计 > 正文

168. Excel Sheet Column Title

2019-11-08 02:38:16
字体:
来源:转载
供稿:网友

题目

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB

Credits: Special thanks to @ifanchu for adding this PRoblem and creating all test cases.

Subscribe to see which companies asked this question.


思路

10进制转26进制,注意’A’是从1开始的,要记得减一


代码

class Solution {public: string convertToTitle(int n) { //本质就是10进制到26进制的转换,从1开始 string result = ""; if(n <= 0) { return result; } int remainder = 0; char temp; while(n) { n--; remainder = n % 26; temp = remainder + 'A'; result = temp + result; n /= 26; } return result; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表