大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~
Given a string columnTitle
that represents the column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Input: columnTitle = "A"
Output: 1
Input: columnTitle = "AB"
Output: 28
Input: columnTitle = "ZY"
Output: 701
Input: columnTitle = "FXSHRXW"
Output: 2147483647
1 <= columnTitle.length <= 7
columnTitle
consists only of uppercase English letters.columnTitle
is in the range ["A", "FXSHRXW"]
.首先先简单的翻译一下题目
给一个columnTitle
的String,他代表的是excel中的column,要把他转换成对应的数字。
作法大致上是这样
columnTitle
的长度-1再减掉for loop跑回圈时的index,加总起来就得到最後的结果啦。class Solution:
def titleToNumber(self, columnTitle: str) -> int:
# ascii "A"=65, "Z"=90
# ord() string to ascii
# chr() ascii to string
# AZ => A*26^1 + Z*26Z^0
# AB => A*26 + B*1
column = 0
for index in range(len(columnTitle)):
column += (ord(columnTitle[index])-64) * (26**(len(columnTitle)-1-index))
return column
int titleToNumber(char * columnTitle){
long int column = 0;
long int multiple = 1;
for (int index=strlen(columnTitle)-1 ; index>=0 ; index--){
column += (( (int)(columnTitle[index])-64 ) * multiple);
multiple *= 26;
}
return column;
}
大家明天见
<<: Day30 - 云端 Study Lab 总结,清除 GCP 专案
RxJava2是一套处理非同步(asynchronous)事件的library,这个library是...
昨天测试完API的第一步之後, 我突然想到我还没说视觉化平台的整个规划, 今天就来谈谈这整个专案的规...
电风扇 ( 控制强度 ) 教学原文参考:电风扇 ( 控制强度 ) 这篇文章会介绍,如何在 Scrat...
seo怎麽做,许多策略可以帮助您更好地排列与您的产品和行业相关的竞争关键字。然而这需要请网路行销公司...
本篇大纲:基本散布图范例、进阶散布图范例 今天的一天一图表,我们要来画 散点图 / 散布图!散布图...