Leetcode:Excel Sheet Column Title
来源:互联网 发布:linux dns配置 编辑:程序博客网 时间:2024/06/05 05:26
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
思路:Because any_pos_int mod 26
should return a number in the interval [0, 25]
, but what we want is a number in the interval [1, 26]
. Thus we have to shift the digit leftward by 1
which meansn-1.
实现代码:
class Solution {public: string convertToTitle(int n) { string res=""; while(n>0){ res=char('A'+(n-1)%26)+res; n=(n-1)/26; } return res; }};
public class Solution { public String convertToTitle(int n) { StringBuilder result = new StringBuilder(); while(n>0){ n--; result.insert(0, (char)('A' + n % 26)); n /= 26; } return result.toString(); }}
0 0
- Leetcode: Excel Sheet Column Title
- [LeetCode]Excel Sheet Column Title
- leetcode:Excel Sheet Column Title
- [LeetCode]Excel Sheet Column Title
- [LeetCode] Excel Sheet Column Title
- [leetcode]Excel Sheet Column Title
- LeetCode--Excel Sheet Column Title
- Leetcode: Excel Sheet Column Title
- 【Leetcode】Excel Sheet Column Title
- [LeetCode] Excel Sheet Column Title
- 【Leetcode】Excel sheet Column title
- LeetCode:《Excel Sheet Column Title》
- leetcode-excel sheet column title
- LeetCode--Excel Sheet Column Title
- LeetCode:Excel Sheet Column Title
- Leetcode: Excel Sheet Column Title
- [leetcode] Excel Sheet Column Title
- LeetCode-Excel Sheet Column Title
- HDU 2546 饭卡
- 矩阵代价问题
- 【BZOJ 2301】 [HAOI2011]Problem b
- HDU 1055 color a tree
- leetcode:Rotate Image菜鸟解法
- Leetcode:Excel Sheet Column Title
- Sort List
- HDU 3415 Max Sum of Max-K-sub-sequence
- 【BZOJ 2820】 YY的GCD
- 黑马程序员——ObjC基础—其他封装
- unity3d 制造自己的水体water effect(一)
- 互联网思维
- HDU-1850-Being a Good Boy in Spring Festival(尼姆博弈)
- ImageLoader----内存缓存篇一