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;
}
相关推荐
MORE_777 分钟前
leecode-灌溉花园-贪心算法and动态规划
算法·贪心算法·动态规划
kyle~12 分钟前
C++--- dlsym 调用封装好的算法动态库的核心工具 <dlfcn.h>
开发语言·c++·算法
superior tigre14 分钟前
C语言中的宏日志打印语法以及相对printf的优点
服务器·c语言·网络
似水এ᭄往昔17 分钟前
【初阶数据结构】--排序算法
数据结构·算法·排序算法
2301_7811435621 分钟前
C语言笔记(四)
c语言·笔记·算法
C羊驼28 分钟前
C语言学习笔记(十四):编译与链接
c语言·开发语言·经验分享·笔记·学习
算法-大模型备案 多米34 分钟前
算法备案算法安全自评估报告模板(精简完善版)
大数据·网络·人工智能·算法·文心一言
Frostnova丶36 分钟前
LeetCode 238 & 2906.构造乘积数组与乘积矩阵
算法·leetcode·矩阵
张槊哲40 分钟前
拆解大语言模型(LLM)的底层推演、架构演进与工业落地
算法
ShineWinsu42 分钟前
对于Linux:git版本控制器和cgdb调试器的解析
linux·c语言·git·gitee·github·调试·cgdb