C#LeetCode刷题之#168-Excel表列名称(Excel Sheet Column Title)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- LeetCode刷题总结-哈希表篇
本文总结在LeetCode上有关哈希表的算法题,推荐刷题总数为12题.具体考察的知识点如下图: 1.数学问题 题号:149. 直线上最多的点数,难度困难 题号:554. 砖墙,难度中等(最大最小边界问 ...
- 力扣(LeetCode)Excel表列名称 个人题解
给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...
- leetcode刷题记录——哈希表
1.两数之和 可以先对数组进行排序,然后使用双指针方法或者二分查找方法.这样做的时间复杂度为 O(NlogN),空间复杂度为 O(1). 用 HashMap 存储数组元素和索引的映射,在访问到 num ...
- Excel表列名称(给定一个正整数,返回它在 Excel 表中相对应的列名称。)
例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... 示例 1: 输入: 1 输出: "A ...
- LeetCode 171. Excel表列序号(Excel Sheet Column Number) 22
171. Excel表列序号 171. Excel Sheet Column Number 题目描述 给定一个 Excel 表格中的列名称,返回其相应的列序号. 每日一算法2019/5/25Day 2 ...
- LeetCode 168. Excel表列名称(Excel Sheet Column Title)
168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- 【leetcode刷题笔记】Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
随机推荐
- C#数据结构与算法系列(二十二):快速排序算法(QuickSort)
1.介绍 快速排序(QuickSort)是对冒泡排序的一种改进,基本思想是:通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另一部分的所有数据都要小,然后再按此方法对这两部分数 ...
- Ethical Hacking - NETWORK PENETRATION TESTING(24)
Detecting suspicious activities using Wireshark You can use make the MAC address of the router to st ...
- scratch编程滑雪者游戏教程
首先我们来看一下效果: 我们从演示中能看出4个角色:企鹅.大树.旗子和装饰用的坎,我们通过键盘操控企鹅滑雪躲避树并捡起旗子,现在我们就来看看是怎么编的吧! 首先我们要画 ...
- Spring当中循环依赖很少有人讲,今天一起来学习!
网上关于Spring循环依赖的博客太多了,有很多都分析的很深入,写的很用心,甚至还画了时序图.流程图帮助读者理解,我看了后,感觉自己是懂了,但是闭上眼睛,总觉得还没有完全理解,总觉得还有一两个坎过不去 ...
- 设计模式:composite模式
目的:使容器和内容具备一致性 实现:将对象组合成树形结构以表示“部分-整体”的层次结构 实例:文件夹中可以包含文件夹也可以包含文件 例子: class Item //接口定义 { public: vi ...
- Linux中内存、CPU使用情况查看
1.背景 在实际生产中我们为了保证系统能稳定运行,我们经常要查看当前的CPU和系统使用情况 建议使用top,简单丰富,快捷 2.使用free查看内存使用情况 3.使用 top查看内存.cpu内存占比 ...
- .Net Core in Docker极简入门(上篇)
目录 前言 开始 环境准备 Docker基础概念 Docker基础命令 Docker命令实践 构建Docker镜像 Dockerfile bulid & run 前言 Docker 是一个开源 ...
- django-rest-framework-源码解析004-三大验证(认证/权限/限流)
三大验证模块概述 在DRF的APIView重写的dispatch方法中, self.initial(request, *args, **kwargs) 这句话就是执行三大验证的逻辑, 点进去可以看到 ...
- anaconda一站式环境的搭建(anaconda、tensorflow、opencv)
搭建人工智能图像处理环境 Anaconda一站式开发环境搭建. 工欲善其事必先利其器,在我们学习之前,我们先要搭建一个属于我们自己的开发环境.我们开发的环境是有anaconda.testflow.op ...
- C# File.Exists 判断系统文件,警惕32位和64位的差异
今天在调试一个Winform程序,使用File.Exists 判断一个已经存在的驱动文件,程序一直返回false.因为驱动文件属于系统目录,心想难道是权限不够导致的?然后用管理员身份运行软件,依然返回 ...