Python 刷Leetcode题库,顺带学英语单词(39)

简介: Python 刷Leetcode题库,顺带学英语单词(39)

Excel Sheet Column Title


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


For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
Examples:
Input: 1
Output: "A"
Input: 28
Output: "AB"
Input: 701
Output: "ZY"



>>> def num2chr(n):
  ret = ''
  while n//26:
    n,m = divmod(n-1,26)
    ret = chr(m+65) + ret
  if n:ret = chr(n+64) + ret
  return ret
>>> num2chr(1)
'A'
>>> num2chr(28)
'AB'
>>> num2chr(701)
'ZY'
>>> num2chr(703)
'AAA'
>>> num2chr(1502)
'BET'
>>> num2chr(16384)
'XFD'
>>> num2chr(17860)
'ZJX'
>>> 



Excel Sheet Column Number


Given a column title as appear in an Excel sheet, return its corresponding column number.     [#171]

For example:


A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Examples:
Input: "A"
Output: 1
Input: "AB"
Output: 28
Input: "ZY"
Output: 701



>>> def chr2num(s):
  return sum([(ord(n)-64)*26**i for i,n in enumerate(list(s)[::-1])])
>>> chr2num('A')
1
>>> chr2num('AB')
28
>>> chr2num('ZY')
701
>>> chr2num('AAA')
703
>>> chr2num('XFD')
16384
>>> 
>>> chr2num('YRH')
17376



目录
相关文章
|
5天前
|
存储 索引 Python
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
31 0
|
6月前
|
测试技术
LeetCode字符串题库 之 罗马数字转整数
LeetCode字符串题库 之 罗马数字转整数
LeetCode字符串题库 之 罗马数字转整数
|
5天前
|
API Python
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
|
5天前
|
存储 JSON 数据可视化
Python期末复习题库(下)——“Python”
Python期末复习题库(下)——“Python”
|
5天前
|
数据采集 IDE 开发工具
Python期末复习题库(上)——“Python”
Python期末复习题库(上)——“Python”
|
5天前
|
算法 索引 Python
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
38 0
|
10月前
|
存储 算法 C++
(C语言版)力扣(LeetCode)题库1-5题解析
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
|
5天前
|
开发者 索引 Python
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
28 0
|
10月前
【Leetcode】题库-爽刷简单题(1)
【Leetcode】题库-爽刷简单题(1)
64 0
|
10月前
|
JSON 数据格式 Python
python解析考试题库数据
应单位要求需要参加某个考试,但考试需要从手机端登陆学习,1000多道题需要挨个刷一遍太过于麻烦,萌生了把题目和答案全部扒下来的想法,再用python做数据的清洗和梳理,最后整合出来所有的考试题库信息。
128 0