LeetCode //C - 171. Excel Sheet Column Number

168. Excel Sheet Column Title

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.

For example:

A -> 1

B -> 2

C -> 3

...

Z -> 26

AA -> 27

.AB -> 28

...

Example 1:

Input: columnTitle = "A"
Output: 1

Example 2:

Input: columnTitle = "AB"
Output: 28

Example 3:

Input: columnTitle = "ZY"
Output: 701

Constraints:
  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].

From: LeetCode

Link: 171. Excel Sheet Column Number


Solution:

Ideas:
  1. The function titleToNumber takes a string columnTitle as input.
  2. It initializes an integer result to 0.
  3. It iterates over each character in the string. For each character, it updates the result by multiplying the current result by 26 (since there are 26 letters in the alphabet) and adding the position of the current character in the alphabet (calculated as *columnTitle - 'A' + 1).
  4. The function returns the final result, which is the corresponding column number.
Code:
c 复制代码
int titleToNumber(char* columnTitle) {
    int result = 0;
    while (*columnTitle) {
        result = result * 26 + (*columnTitle - 'A' + 1);
        columnTitle++;
    }
    return result;
}
相关推荐
郝学胜-神的一滴21 小时前
OpenGL中的glDrawArrays函数详解:从基础到实践
开发语言·c++·程序人生·算法·游戏程序·图形渲染
_OP_CHEN21 小时前
【算法基础篇】(三十四)图论基础深度解析:从概念到代码,玩转图的存储与遍历
算法·蓝桥杯·图论·dfs·bfs·算法竞赛·acm/icpc
Trouvaille ~21 小时前
【LInux】进程程序替换与shell实现:从fork到exec的完整闭环
linux·运维·c语言·c++·ssh·进程替换·基础入门
SETH·XU21 小时前
简记:关于net-snmp中engineid冲突全局etimelist的赋值情况
c语言·网络·net-snmp·snmpv3·engineid冲突
王璐WL21 小时前
【数据结构】栈和队列及相关算法题
数据结构·算法
麒qiqi21 小时前
Linux 线程(POSIX)核心教程
linux·算法
Zhi.C.Yue21 小时前
React 的桶算法详解
前端·算法·react.js
小热茶21 小时前
浮点数计算专题【五、 IEEE 754 浮点乘法算法详解---基于RISCV的FP32乘法指令在五级流水线的运行分析与SystemC实现】
人工智能·嵌入式硬件·算法·systemc
Giser探索家21 小时前
卫星遥感数据核心参数解析:空间分辨率与时间分辨率
大数据·图像处理·人工智能·深度学习·算法·计算机视觉
q_30238195561 天前
破局路侧感知困境:毫米波雷达+相机融合算法如何重塑智能交通
数码相机·算法