LeetCode-168.Excel表格名称

分析
26进制的问题。
需要注意每“位”都是从0开始,因此需要先对 n n 减一。

代码

class Solution {
public:
	string convertToTitle(int n) {
		string res;
		while (n--) {
			res += n % 26 + 'A';
			n /= 26;
		}
		reverse(res.begin(), res.end());
		return res;
	}
};
发布了189 篇原创文章 · 获赞 107 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/cprimesplus/article/details/103338095