问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3850 访问。

给定一个正整数,返回它在 Excel 表中相对应的列名称。

例如,

1 -> A

    2 -> B

    3 -> C

    ...

    26 -> Z

    27 -> AA

    28 -> AB 

    ...

输入: 1

输出: "A"

输入: 28

输出: "AB"

输入: 701

输出: "ZY"


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 

    ...

Input: 1

Output: "A"

Input: 28

Output: "AB"

Input: 701

Output: "ZY"


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3850 访问。

public class Program {

    public static void Main(string[] args) {
var n = 701;
var res = ConvertToTitle(n);
Console.WriteLine(res); n = 678;
res = ConvertToTitle2(n);
Console.WriteLine(res); n = 12345;
res = ConvertToTitle3(n);
Console.WriteLine(res); Console.ReadKey();
} private static string ConvertToTitle(int n) {
if(n <= 26) return ((char)(n + 'A' - 1)).ToString();
if(n % 26 == 0) {
return ConvertToTitle(n / 26 - 1) + 'Z';
} else {
return ConvertToTitle(n / 26) + ConvertToTitle(n % 26);
}
} private static string ConvertToTitle2(int n) {
if(n <= 0) return "";
return ConvertToTitle((n - 1) / 26) + (char)((n - 1) % 26 + 'A');
} private static string ConvertToTitle3(int n) {
var res = string.Empty;
while(n > 0) {
var s = (char)((n - 1) % 26 + 'A');
res = s + res;
n = (n - 1) / 26;
}
return res;
} }

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3850 访问。

ZY
ZB
RFU

分析:

显而易见,以上3种算法的时间复杂度均为:  。

C#LeetCode刷题之#168-Excel表列名称(Excel Sheet Column Title)的更多相关文章

  1. [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title

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

  2. LeetCode刷题总结-哈希表篇

    本文总结在LeetCode上有关哈希表的算法题,推荐刷题总数为12题.具体考察的知识点如下图: 1.数学问题 题号:149. 直线上最多的点数,难度困难 题号:554. 砖墙,难度中等(最大最小边界问 ...

  3. 力扣(LeetCode)Excel表列名称 个人题解

    给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...

  4. leetcode刷题记录——哈希表

    1.两数之和 可以先对数组进行排序,然后使用双指针方法或者二分查找方法.这样做的时间复杂度为 O(NlogN),空间复杂度为 O(1). 用 HashMap 存储数组元素和索引的映射,在访问到 num ...

  5. Excel表列名称(给定一个正整数,返回它在 Excel 表中相对应的列名称。)

    例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... 示例 1: 输入: 1 输出: "A ...

  6. LeetCode 171. Excel表列序号(Excel Sheet Column Number) 22

    171. Excel表列序号 171. Excel Sheet Column Number 题目描述 给定一个 Excel 表格中的列名称,返回其相应的列序号. 每日一算法2019/5/25Day 2 ...

  7. LeetCode 168. Excel表列名称(Excel Sheet Column Title)

    168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...

  8. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  9. 【leetcode刷题笔记】Excel Sheet Column Number

    Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...

  10. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

随机推荐

  1. OSCP Learning Notes - Buffer Overflows(2)

    Finding the Offset 1. Use the Metasploite pattern_create.rb tool to create 5900 characters. /usr/sha ...

  2. 洛谷P2365/5785 任务安排 题解 斜率优化DP

    任务安排1(小数据):https://www.luogu.com.cn/problem/P2365 任务安排2(大数据):https://www.luogu.com.cn/problem/P5785 ...

  3. HashTable、HashMap与ConCurrentHashMap源码解读

    HashMap 的数据结构 ​ hashMap 初始的数据结构如下图所示,内部维护一个数组,然后数组上维护一个单链表,有个形象的比喻就是想挂钩一样,数组脚标一样的,一个一个的节点往下挂. ​ 我们可以 ...

  4. python元编程(metaclass)

    Python元编程就是使用metaclass技术进行编程,99%的情况下不会使用,了解即可. Python中的类和对象 对于学习Python和使用Python的同学,你是否好奇过Python中的对象究 ...

  5. 微信PC端多开的秘密

    微信电脑端也能多开 昨天,偶然从好朋友小林(微信公众号:小林Coding)处得知,他的电脑居然可以同时上两个微信号. 手机端多开微信我知道,像华为.小米等手机系统都对此做了支持,不过在运行Window ...

  6. Python对列表去重的各种方法

    一.循环去重   二.用 set() 去重 1.set()对list去重 2.list 是有序的,用 sort() 把顺序改回来  三.利用 dict 的属性来去重 1.用 dict 的 fromke ...

  7. IDEA中搭建项目环境

    ladies and gentlemen,Welcome to my blog! 本文主要在IDEA中搭建项目环境. 有问题和指正,欢迎下方留言~ 1. 使用GitLab将项目下载下来   1.1 选 ...

  8. .Net微服务实战之CI/CD

    系列文章 .Net微服务实战之技术选型篇 .Net微服务实战之技术架构分层篇 .Net微服务实战之DevOps篇 .Net微服务实战之负载均衡(上) 相关源码:https://github.com/S ...

  9. 06_Python基础课程

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"软件测试"获取视频和教程资料! b站在线视频 Pyth ...

  10. vue多个路由复用同一个组件的跳转问题(this.router.push)

    因为router-view传参问题无法解决,比较麻烦. 所以我采取的是@click+this.router.push来跳转 但是现在的问题是跳转后,url改变了,但是页面的数据没有重新渲染,要刷新才可 ...